[] slicing is provided purely as a convience for slicing the rows: http://pandas.pydata.org/pandas-docs/dev/indexing.html#slicing-ranges[] is overloaded quite a lot, but primarily functions as a column selectorloc behavior of not raising an error is exactly what it is supposed to do. Pandas maintains integrity of what you are trying to do,this use of loc is equivalent of a reindexing operation.
If you are truly interested in having only thoses dates in the index, then just do the following:In [11]: df.loc[df.index & dates]
Out[11]:
0 1 2 3 4 5 6 7 8 9
2014-01-01 1 1 1 1 1 1 1 1 1 1
2014-02-01 1 1 1 1 1 1 1 1 1 1
2014-03-01 1 1 1 1 1 1 1 1 1 1
try this:
In [10]: df.index.equals(dates)
Out[10]: FalseIn [11]: (df.index & dates).equals(dates)
Out[11]: False(== is not supported on indexing objects for a variaty of reasons)pandas does have different behavior if a single item is presented versus a list/boolean indexer/slicethe former will KeyError if it doesn't exist, while the latter do not. this has been the case as far back as I can remember.You need to check your self if you want ALL of the objects to be present, which IMHO is actually a special case.you can also doIn [13]: df.index.isin(dates)
Out[13]: array([ True, True, True], dtype=bool)In [14]: dates.isin(df.index)
Out[14]: array([ True, True, True, False], dtype=bool)and index using that if you want.
In [15]: dates1 = pd.date_range('01-Jan-2014', '01-Jan-2016', freq='H')[0:-1]
...: dates2 = pd.date_range('01-Jan-2014', '01-Jan-2016', freq='D')
In [16]: %timeit (dates1 & dates2).equals(dates2)
1000 loops, best of 3: 604 µs per loop
In [17]: %timeit dates2.isin(dates1).all()
100 loops, best of 3: 4.3 ms per loop
-Dave
--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.