I have a list of dates, but I can't just use df['date'].isin(list_of_dates)
because I also want any rows whose date is within 10 days of a date in my list. For example, if my dataframe had 5 rows whose dates were
date
02-04-21
30-03-21
22-03-21
18-03-21
12-03-21
and list_of_dates = ["30-03-21", "05-03-21"]
then I would want three first three rows because they are within 10 days of "30-03-21"
, and also the last row because it is within 10 days of "05-03-21"
. But not the fourth row.
I would use pandas.date_range() to generate all the possible dates.
list_of_dates = ["30-03-21", "05-03-21"]
set_of_dates_expand = set()
for date in list_of_dates:
set_of_dates_expand.update(pd.date_range(start=date, periods=10))
set_of_dates_expand.update(pd.date_range(end=date, periods=10))
df['date'].isin(set_of_dates_expand)
Target is change list ["30-03-21", "05-03-21"] to below list
Output
2021-03-30 0
2021-03-31 0
2021-04-01 0
2021-04-02 0
2021-04-03 0
2021-04-04 0
2021-04-05 0
2021-04-06 0
2021-04-07 0
2021-04-08 0
use reample() function
list_of_dates = ["30-03-21", "05-03-21"]
df=pd.DataFrame({'Date':list_of_dates})
df['Date']=pd.to_datetime(df['Date'])
s1=df['Date'].append(
pd.Series(df.loc[0,'Date']+pd.Timedelta('9D'))).append(
pd.Series(df.loc[1,'Date']+pd.Timedelta('9D')))
df1=df1.reset_index().set_index(0)
df1=df1.sort_index()
target=df1.iloc[:2,:].resample('D').fillna(method='ffill').append(
df1.iloc[2:,:].resample('D').fillna(method='ffill'))
Good luck~
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.