Issue
I have the following dates.
start = '2013-12-03'
end = '2023-07-17'
date = '05-02'
I want to get only the dates that match date
between start
and end
.
desired output:
DatetimeIndex(['2014-05-02', '2015-05-02', '2016-05-02', '2017-05-02',
'2018-05-02', '2019-05-02', '2020-05-02', '2021-05-02',
'2022-05-02', '2023-05-02'],
dtype='datetime64[ns]', name=0, freq=None)
I used the following code.
import pandas as pd
m, d = map(int, date.split('-'))
idx = pd.date_range(start[:5] + '01-01', end[:5] + '12-31', freq='12M')
pd.Index(idx.to_frame()[0].add(pd.DateOffset(month=m, day=d))[lambda x: x.between(start, end)])
Is there a simpler way?
There was a way to use freq='AS-MAY'
, but it required modification depending on the case when in the same month.
i edit @Suraj Shourie answer more simply
start_year = pd.to_datetime(start).year
dates = pd.date_range(f'{start_year}-{date}', end, freq=pd.DateOffset(months=12))
dates[dates >= start]
Solution
I'm not sure if this is a more straightforward way, but using this you won't get the performance warning your code gives. I tried to make it more generic and use a simple boolean filter instead of the lambda filter:
import pandas as pd
start = '2013-12-03'
end = '2023-07-17'
date = '05-02'
start_year = pd.to_datetime(start).year
end_year = pd.to_datetime(end).year
dates = pd.date_range(start=f"{start_year}-{date}", end=f"{end_year}-{date}", freq=pd.DateOffset(months=12))
dates[(dates >= start) & (dates <= end)] # Filter
Output:
DatetimeIndex(['2014-05-02', '2015-05-02', '2016-05-02', '2017-05-02',
'2018-05-02', '2019-05-02', '2020-05-02', '2021-05-02',
'2022-05-02', '2023-05-02'],
dtype='datetime64[ns]', freq='<DateOffset: months=12>')
Answered By - Suraj Shourie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.