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

我正在尝试聚合每天 15 分钟时间间隔的数据。当我检查索引的数据类型时,它位于 datetime64[ns]

完成者:

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

但是,我仍然得到错误:

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

我运行以下代码进行聚合:

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

有人知道出了什么问题以及如何解决吗?

而不是:

liquidation_1d_df = df.set_index('timestamp')

尝试:

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

或者不想改变原来的df

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