仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有"Int64Index"的实例 [英] Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

查看:176
本文介绍了仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有"Int64Index"的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对该数据帧的此 Timestamp 列进行重新采样:

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

这就是我所做的:

  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.

推荐答案

通过DatetimeIndex .set_index.html"rel =" noreferrer> DataFrame.set_index -上采样和下采样的解决方案:

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

可以使用参数on进行降采样:

For downsampling is possible use parameter on:

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

要删除所有重复的Timestamp行,请将参数subset添加到

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()

这篇关于仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有"Int64Index"的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆