Pandas DatetimeIndex truncation error

I have a pandas dataframe df

:

Out[16]:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 269850 entries, 2012-12-19 16:15:36 to 2012-12-20 14:36:55
Data columns:
X1    269850  non-null values
X2      269848  non-null values
X3      269848  non-null values
dtypes: float64(2), object(1)

      

And I would like to slice the dataframe to return a four hour data window from 2012-12-20 05:00:00

to2012-12-20 09:00:00

When I try:

Slicedf = df.truncate(before='12/20/2012 05:00:00',after='12/20/2012 09:00:00')

      

The following error occurred:

KeyError: datetime.datetime(2012, 12, 20, 5, 0)

      

I also tried (from Pandas slicing DataFrame by day / hour / minute ):

from datetime import datetime
x=datetime(2012,12,20,5,0,0)
y=datetime(2012,12,20,9,0,0)
Slicedf = df.ix[x:y]

      

which returns the same error.

+3


source to share


1 answer


df

was created by concatenating multiple data files together using a function concat

.



df = df.sort()

before truncation catches the error.

+3


source







All Articles