Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas multi index slicing "Level type mismatch"

I moved to pandas version 0.17 from 0.13.1 and I get some new errors on slicing.

>>> df
         date  int  data
0  2014-01-01    0     0
1  2014-01-02    1    -1
2  2014-01-03    2    -2
3  2014-01-04    3    -3
4  2014-01-05    4    -4
5  2014-01-06    5    -5
>>> df.set_index("date").ix[datetime.date(2013,12,30):datetime.date(2014,1,3)]
            int  data
date                 
2014-01-01    0     0
2014-01-02    1    -1
2014-01-03    2    -2
>>> df.set_index(["date","int"]).ix[datetime.date(2013,12,30):datetime.date(2014,1,3)]
Traceback (most recent call last):
...
TypeError: Level type mismatch: 2013-12-30

it's working fine with 0.13.1, and it's seems specific for multi-index with date. Am I doing something wrong here?

like image 772
clement Avatar asked Oct 31 '22 15:10

clement


1 Answers

This error occurs because you're trying to slice on dates (labels) that are not included in the index. To solve this Level mismatch error and return values within a range that may or may not be within a df multiindex use:

df.loc[df.index.get_level_values(level = 'date') >= datetime.date(2013,12,30)] 
# You can use a string also i.e. '2013-12-30'

get_level_values() and the comparison operator set a mask of True/False index values for the indexer.

Slicing with a string or date object normally works in Pandas with a single index regardless of if the string is in the index, but doesn't work on multiindex dataframes. Though you attempted to set the index from 2013-12-30 to 2014-01-03 with the datetime.date(2013,12,30) : datetime.date(2014,1,3) set_index call, the resulting df index was from 2014-01-01 to 2014-01-03. One correct way to set the index for those dates including 2013-12-30 would be to set the index as a date range using either strings for datetime objects like:

df.set_index("date").loc[pd.date_range('2013-12-30', '2014-12-03')]
like image 157
David Darby Avatar answered Nov 15 '22 05:11

David Darby