Python 加入一个日期为+;时间戳索引和带有日期索引的数据帧
熊猫有没有办法加入一个数据帧Python 加入一个日期为+;时间戳索引和带有日期索引的数据帧,python,pandas,Python,Pandas,熊猫有没有办法加入一个数据帧df,它的索引是datetime64[ns]格式,比如'2014-09-26 15:15:00',而另一个数据帧的索引是datetime64[ns]格式,比如'2014-09-26,但没有时间戳 例如: print df1 Vol 2011-01-05 09:45:00 0.333315 2011-01-05 09:45:00 0.228341 2011-01-05 10:00:00
df
,它的索引是datetime64[ns]
格式,比如'2014-09-26 15:15:00'
,而另一个数据帧的索引是datetime64[ns]
格式,比如'2014-09-26
,但没有时间戳
例如:
print df1
Vol
2011-01-05 09:45:00 0.333315
2011-01-05 09:45:00 0.228341
2011-01-05 10:00:00 0.207289
和df2
macro_ind
2010-01-05 0.216485
2010-01-06 0.201550
2010-01-07 0.187641
2010-01-08 0.176686
我尝试使用df1.join(df2)
,希望得到:
Vol macro_ind
2011-01-05 09:45:00 0.333315 0.216485
2011-01-05 09:45:00 0.228341 0.216485
2011-01-05 10:00:00 0.207289 0.216485
但是没有成功。。。我可以使用
pd.merge
,但这会迫使我创建一个日期列并重置索引等等。。。很多我希望避免的步骤。我假设宏索引也注明了2011
。我建议将相应的time
列设置为普通列,如下所示:
date:
date macro_ind
0 2011-01-05 0.216485
1 2011-01-06 0.201550
2 2011-01-07 0.187641
3 2011-01-08 0.176686
time:
time Vol
0 2011-01-05 09:45:00 0.333315
1 2011-01-05 09:45:00 0.228341
2 2011-01-05 10:00:00 0.207289
然后,进行以下工作:
time_df['date'] = time_df.time.apply(lambda x: x.date())
date_df['date'] = date_df.date.apply(lambda x: x.date())
time_df = time_df.merge(date_df, how='left', on='date').drop('date', axis=1)
time Vol macro_ind
0 2011-01-05 09:45:00 0.333315 0.216485
1 2011-01-05 09:45:00 0.228341 0.216485
2 2011-01-05 10:00:00 0.207289 0.216485
是的,我以前也用过类似的东西。join
没有任何功能?很抱歉,我不知道如何将DateTimeIndex
临时转换为date
。也许其他人知道。