Make sure the two Pandas DatetimeIndexes match?

I ran into a problem when comparing two DatetimeIndex with different lengths in assert as follows:

 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() 

I get an error message:

 --------------------------------------------------------------------------- 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' 

Which is good if it is not implemented yet, but is there any way pandas do it?

Note: I have successfully used the following:

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

So the following also works:

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

But I would like to know if there is a more pandas th way to do this.

+4
source share
2 answers
 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 
+5
source

You now have a pandas.Index.Difference that can satisfy your needs.

It is so simple:

 idx_common = idx1.difference(idx2) 
0
source

All Articles