Home > Back-end >  Index is in datetime64[ns] but still get TypeError: Only valid with DatetimeIndex
Index is in datetime64[ns] but still get TypeError: Only valid with DatetimeIndex

Time:05-29

I'm trying to aggregate data for a daily and 15 min time interval. When I check the data type of the index it is in datetime64[ns]

Done this by:

df['timestamp'] = pd.to_datetime(df['timestamp'])
liquidation_1d_df = df.set_index('timestamp')
print(liquidation_1d_df.index.dtype)

However, I still get the error:

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

I run the following code for aggregation:

liquidation_1d_df.groupby([pd.Grouper(key='timestamp', freq='D'), 'exchange', 'side']).agg(total_liq = ('amount', 'sum'), avg_liq = ('amount', 'mean'))

Anyone an idea what is going wrong and how it can be solved?

CodePudding user response:

Instead of :

liquidation_1d_df = df.set_index('timestamp')

Try:

df.set_index('timestamp',inplace=True)
print(df.index.dtype)

Or if you don't want to change the original df

liquidation_1d_df = df
liquidation_1d_df.set_index('timestamp',inplace=True)
print(liquidation_1d_df.index.dtype)
  • Related