确保两个Pandas DatetimeIndexes相同?

时间:2013-01-01 16:19:09

标签: python pandas

DatetimeIndex中比较两个assert不同长度的In [1]: idx1 = pd.date_range('2010-01-01','2010-12-31',freq='D') In [2]: idx2 = pd.date_range('2010-01-01','2010-11-01',freq='D') In [3]: assert (idx1 == idx2).all() 时,我遇到了一个问题,如下所示:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-ad2cfd6d11c2> in <module>()
----> 1 assert (idx1 == idx2).all()

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.10.1.dev_dcd9df7-py2.7-macosx-10.8-x86_64.egg/pandas/tseries/index.pyc in wrapper(self, other)
     75         result = func(other)
     76 
---> 77         return result.view(np.ndarray)
     78 
     79     return wrapper

AttributeError: 'NotImplementedType' object has no attribute 'view'

我收到错误:

In [3]: assert list(idx1) == list(idx2)

如果没有实现,这没关系,但是有一些 pandas 这样做吗?

注意: 我成功地使用了以下内容:

In [3]: assert list(df.index) == list(testindex)

因此,以下内容也有效:

pandas

但我想知道是否有更多的{{1}} - ish方式。

2 个答案:

答案 0 :(得分:5)

In [1]: import pandas as pd

In [2]: idx1 = pd.date_range('2010-01-01','2010-12-31',freq='D')

In [3]: idx2 = pd.date_range('2010-01-01','2010-11-01',freq='D')

In [4]: idx3 = pd.date_range('2010-01-01','2010-12-31',freq='D')

In [5]: help(idx1.equals)
Help on method equals in module pandas.tseries.index:

equals(self, other) method of pandas.tseries.index.DatetimeIndex instance
    Determines if two Index objects contain the same elements.


In [6]: print(idx1.equals(idx2))
False

In [7]: print(idx1.equals(idx3))
True

答案 1 :(得分:0)

您现在拥有pandas.Index.Difference,可能符合您的需求。

就这么简单:

idx_common = idx1.difference(idx2)