Filter pivot table on datetime index column

时间:2016-04-04 17:57:08

标签: python-2.7 datetime pandas filter slice

Hi I want to slice a pivot table on specific dates and found this topic where Orange1 gives an answer for columns with datetime values. To be complete here my data and my code:

Df_Pivot:

                                                    max
                                     DateTime_End
    DateTime_Start       Date                  
    2015-03-01 09:18:08  2015-03-01 2015-03-01 09:58:00
    2015-03-01 08:10:26  2015-03-01 2015-03-01 08:33:00
    2015-03-01 09:13:15  2015-03-01 2015-03-01 09:28:00
    2015-03-01 09:01:06  2015-03-01 2015-03-01 09:20:00
    2015-03-01 11:53:37  2015-03-01 2015-03-01 12:00:00

I would like to filter DateTime_Start on date. Now I have done this with the extra column date which is datetime.date() converted into string(I add the date column later). However, I think it is more elegant to filter the index column DateTime_Start only on the date.

Is this possible?

Inspired by this Topic with the solution of Orange1, this topic on datetime import and this topic on too many indexers, constructed this:

from datetime import datetime, date
import pandas as pd
idx = pd.IndexSlice
data['Df_New'] = data['Df_Pivot'].loc[idx[date(year=2015,month=4,day=15),:],:]

It returns this:

   File "/anaconda/lib/python2.7/site-packages/pandas/tseries/index.py", line 1369, in get_loc
    raise KeyError(key)
KeyError: datetime.date(2015, 4, 15)

0 个答案:

没有答案