【发布时间】:2016-10-05 19:53:42
【问题描述】:
给定以下数据框:
import pandas as pd
df=pd.DataFrame({'A':['a','b','c'],
'first_date':['2015-08-31 00:00:00','2015-08-24 00:00:00','2015-08-25 00:00:00']})
df.first_date=pd.to_datetime(df.first_date) #(dtype='<M8[ns]')
df['last_date']=pd.to_datetime('5/6/2016') #(dtype='datetime64[ns]')
def fnl(x):
l = pd.date_range(x.loc['first_date'], x.loc['last_date'], freq='B')
return pd.Series([l])
df['range'] = df.apply(fnl, axis=1)
df
A first_date last_date range
0 a 2015-08-31 2016-05-06 DatetimeIndex(['2015-08-31', '2015-09-01', '20...
1 b 2015-08-24 2016-05-06 DatetimeIndex(['2015-08-24', '2015-08-25', '20...
2 c 2015-08-25 2016-05-06 DatetimeIndex(['2015-08-25', '2015-08-26', '20...
对于落入其相应范围的每个日期(即如果 exc['A'] 中的日期超出了 df['A'] 中的对应范围,显然不能排除。
exc=pd.DataFrame({'A':['a','a','b','b','c','c'],
'Exclusions':['2014-12-30 00:00:00','2015-08-31 00:00:00',\
'2015-08-25 00:00:00','2015-10-20 00:00:00',\
'2015-08-26 00:00:00','2016-10-05 00:00:00']
})
exc
A Exclusions
0 a 2014-12-30 00:00:00
1 a 2015-08-31 00:00:00
2 b 2015-08-25 00:00:00
3 b 2015-10-20 00:00:00
4 c 2015-08-26 00:00:00
5 c 2016-10-05 00:00:00
想要的结果:
A first_date last_date range
0 a 2015-08-31 2016-05-06 DatetimeIndex(['2015-09-01', '2015-09-02', '20...
1 b 2015-08-24 2016-05-06 DatetimeIndex(['2015-08-24', '2015-08-26', '20...
2 c 2015-08-25 2016-05-06 DatetimeIndex(['2015-08-25', '2015-08-27', '20...
提前致谢!
【问题讨论】:
标签: python-3.x pandas indexing dataframe date-range