Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

I'm trying to resample this Timestamp column of this Dataframe:

  Transit.head():

      Timestamp                            Plate           Gate
  0 2013-11-01 21:02:17 4f5716dcd615f21f658229a8570483a8    65
  1 2013-11-01 16:12:39 0abba297ac142f63c604b3989d0ce980    64
  2 2013-11-01 11:06:10 faafae756ce1df66f34f80479d69411d    57

And Here's What I've Done:

  Transit.drop_duplicates(inplace=True)
  Transit.Timestamp = pd.to_datetime(Transit.Timestamp)
  Transit['Timestamp'].resample('1H').pad()

But I got This Error:

  Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

Any Suggestion Would Be Much Appreciated.

like image 651
Dimi Avatar asked Apr 02 '19 11:04

Dimi


People also ask

Is DF_orders index a datetimeindex or int64index?

Apparently, sometimes your df_orders.index is a DatetimeIndex and sometimes it is an Int64Index. You should add a check like if df_orders.index.dtype != pd.DatetimeIndex ....

How to use existing date column as index in Dataframe?

Use existing date column as index If your dataframe already has a date column, you can use use it as an index, of type DatetimeIndex:

How to manipulate date and time values in pandas Dataframe?

For more examples on how to manipulate date and time values in pandas dataframes, see Pandas Dataframe Examples: Manipulating Date and Time If your dataframe already has a date column, you can use use it as an index, of type DatetimeIndex: df: original dataframe. Note that added for the missing periods.


1 Answers

Create DatetimeIndex by DataFrame.set_index - solution for upsampling and downsampling:

df = Transit.set_index('Timestamp').resample('1H').pad()
print (df)
                                                Plate  Gate
Timestamp                                                  
2013-11-01 11:00:00                               NaN   NaN
2013-11-01 12:00:00  faafae756ce1df66f34f80479d69411d  57.0
2013-11-01 13:00:00  faafae756ce1df66f34f80479d69411d  57.0
2013-11-01 14:00:00  faafae756ce1df66f34f80479d69411d  57.0
2013-11-01 15:00:00  faafae756ce1df66f34f80479d69411d  57.0
2013-11-01 16:00:00  faafae756ce1df66f34f80479d69411d  57.0
2013-11-01 17:00:00  0abba297ac142f63c604b3989d0ce980  64.0
2013-11-01 18:00:00  0abba297ac142f63c604b3989d0ce980  64.0
2013-11-01 19:00:00  0abba297ac142f63c604b3989d0ce980  64.0
2013-11-01 20:00:00  0abba297ac142f63c604b3989d0ce980  64.0
2013-11-01 21:00:00  0abba297ac142f63c604b3989d0ce980  64.0

For downsampling is possible use parameter on:

df = Transit.resample('D', on='Timestamp').mean()
print (df)
            Gate
Timestamp       
2013-11-01    62

EDIT: For remove all rows with duplicated Timestamp add parameter subset to DataFrame.drop_duplicates:

Transit.drop_duplicates(subset=['Timestamp'], inplace=True)
Transit.Timestamp = pd.to_datetime(Transit.Timestamp)
df = Transit.set_index('Timestamp').resample('1H').pad()
like image 103
jezrael Avatar answered Sep 22 '22 07:09

jezrael