Pandas 将多个数据帧与时间戳索引对齐
Posted
技术标签:
【中文标题】Pandas 将多个数据帧与时间戳索引对齐【英文标题】:Pandas aligning multiple dataframes with TimeStamp index 【发布时间】:2014-12-09 13:18:35 【问题描述】:在过去的几天里,这一直是我生活的祸根。我有许多 Pandas 数据框,其中包含频率不规则的时间序列数据。我尝试将它们对齐到单个数据框中。
下面是一些代码,带有代表性的数据框 df1
、df2
和 df3
(我实际上有 n=5,并且希望有一个适用于所有 n>2
的解决方案):
# df1, df2, df3 are given at the bottom
import pandas as pd
import datetime
# I can align df1 to df2 easily
df1aligned, df2aligned = df1.align(df2)
# And then concatenate into a single dataframe
combined_1_n_2 = pd.concat([df1aligned, df2aligned], axis =1 )
# Since I don't know any better, I then try to align df3 to combined_1_n_2 manually:
combined_1_n_2.align(df3)
error: Reindexing only valid with uniquely valued Index objects
我知道为什么会出现此错误,因此我删除了 combined_1_n_2
中的重复索引并重试:
combined_1_n_2 = combined_1_n_2.groupby(combined_1_n_2.index).first()
combined_1_n_2.align(df3) # But stll get the same error
error: Reindexing only valid with uniquely valued Index objects
为什么会出现此错误?即使这有效,它也是完全手动且丑陋的。如何对齐 >2 个时间序列并将它们组合在一个数据帧中?
数据:
df1 = pd.DataFrame( 'price' : [62.1250,62.2500,62.2375,61.9250,61.9125 ],
index = [pd.DatetimeIndex([datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f')])[0]
for s in ['2008-06-01 06:03:59.614000', '2008-06-01 06:03:59.692000',
'2008-06-01 06:15:42.004000', '2008-06-01 06:15:42.083000','2008-06-01 06:17:01.654000' ] ])
df2 = pd.DataFrame('price': [241.0625, 241.5000, 241.3750, 241.2500, 241.3750 ],
index = [pd.DatetimeIndex([datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f')])[0]
for s in ['2008-06-01 06:13:34.524000', '2008-06-01 06:13:34.602000',
'2008-06-01 06:15:05.399000', '2008-06-01 06:15:05.399000','2008-06-01 06:15:42.082000' ] ])
df3 = pd.DataFrame('price': [67.656, 67.875, 67.8125, 67.75, 67.6875 ],
index = [pd.DatetimeIndex([datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f')])[0]
for s in ['2008-06-01 06:03:52.281000', '2008-06-01 06:03:52.359000',
'2008-06-01 06:13:34.848000', '2008-06-01 06:13:34.926000','2008-06-01 06:15:05.321000' ] ])
【问题讨论】:
【参考方案1】:您的具体错误是由于combined_1_n_2
的列名重复(两列都将命名为“价格”)。您可以重命名列,然后第二个对齐就可以了。
另一种方法是链接join
运算符,它合并索引上的帧,如下所示。
In [23]: df1.join(df2, how='outer', rsuffix='_1').join(df3, how='outer', rsuffix='_2')
Out[23]:
price price_1 price_2
2008-06-01 06:03:52.281000 NaN NaN 67.6560
2008-06-01 06:03:52.359000 NaN NaN 67.8750
2008-06-01 06:03:59.614000 62.1250 NaN NaN
2008-06-01 06:03:59.692000 62.2500 NaN NaN
2008-06-01 06:13:34.524000 NaN 241.0625 NaN
2008-06-01 06:13:34.602000 NaN 241.5000 NaN
2008-06-01 06:13:34.848000 NaN NaN 67.8125
2008-06-01 06:13:34.926000 NaN NaN 67.7500
2008-06-01 06:15:05.321000 NaN NaN 67.6875
2008-06-01 06:15:05.399000 NaN 241.3750 NaN
2008-06-01 06:15:05.399000 NaN 241.2500 NaN
2008-06-01 06:15:42.004000 62.2375 NaN NaN
2008-06-01 06:15:42.082000 NaN 241.3750 NaN
2008-06-01 06:15:42.083000 61.9250 NaN NaN
2008-06-01 06:17:01.654000 61.9125 NaN NaN
【讨论】:
谢谢,这太令人惊讶了;所以如果我进行链连接,align()
是不必要的?
正确,join
为您处理索引逻辑。
谢谢,出于好奇:您能否简要说明一下何时可能需要使用 align()?因为在我看来,join() 似乎一次性处理了对齐和连接。
我能想到的实际案例不多,但如果由于某种原因只需要对齐索引,使用align
会节省一点时间。以上是关于Pandas 将多个数据帧与时间戳索引对齐的主要内容,如果未能解决你的问题,请参考以下文章
有没有一种pythonic方法可以将日期时间上的数据帧与具有不规则日期时间戳的数据对合并