Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DatetimeIndex truncate 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 window of data from 2012-12-20 05:00:00 to 2012-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 occurs:

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

I have also tried (from Pandas DataFrame slicing 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 exact same error.

like image 282
Clayton Avatar asked Jan 31 '13 15:01

Clayton


2 Answers

You might need to change it to:

df = df.sort_values() or df = df.sort_index() before the truncate function in order to fix the error in later versions of Pandas.

So your code would look like: df = df.sort_index().truncate(before='12/20/2012 05:00:00', after='12/20/2012 09:00:00')

sort was deprecated for DataFrames in favor of needing to use either sort_values or sort_index. You can find more details about each in the documentation here and here, respectively.

like image 163
Halee Avatar answered Oct 15 '22 21:10

Halee


df was created by concatenating multiple dataframes together using the concat function.

df = df.sort() before truncation fixed the error.

like image 31
Clayton Avatar answered Oct 15 '22 21:10

Clayton