Pandas数据帧重新索引各自日期时间戳记索引的并集

时间:2015-11-25 23:43:29

标签: python pandas time-series

我有两个数据帧。每个数据帧中有两个相同的列。我可以很好地绘制它,日期时间正确显示为x轴上的索引。

dfEURUSD的索引'TimeStamp'与dfGBPUSD的数据帧不同。我试图让它们具有相同的长度并重新采样丢失的数据,以便两个时间序列的长度相同。但是我遇到了麻烦:

def union(a, b):
    """ return the union of two lists """
    return list(set(a) | set(b))


print(len(dfEURUSD.index))
print(len(dfGBPUSD.index))
newindex = union(dfEURUSD.index, dfGBPUSD.index)
newindex = sorted(set(newindex))
print(len(newindex))

print(newindex)

这给出了这个输出

151527
113903
264664

[Timestamp('2015-11-12 17:49:15.512000'), Timestamp('2015-11-12 17:49:15.523000'), Timestamp('2015-11-12 17:49:15.540000'), Timestamp('2015-11-12 17:49:15.727000'), Timestamp('2015-11-12 17:49:16.059000'), Timestamp('2015-11-12 17:49:16.301000'), etc...

如果我尝试使用

重新编制索引
dfEURUSD.reindex(newindex, method='ffill')
dfGBPUSD.reindex(newindex, method='ffill')

给出错误:

File "/home/idf/anaconda3/lib/python3.5/site-packages/pandas/core/index.py", line 2133, in reindex
    raise ValueError("cannot reindex a non-unique index "

ValueError: cannot reindex a non-unique index with a method or limit

不确定我做错了什么?

0 个答案:

没有答案
相关问题