gpt4 book ai didi

python-3.x - 仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但获得了 'Int64Index'的实例

转载 作者:行者123 更新时间:2023-12-01 00:29:14 25 4
gpt4 key购买 nike

我正在尝试对该数据帧的时间戳列进行重新采样:

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

但是我得到了这个错误:
  Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

任何建议将不胜感激。

最佳答案

通过 DatetimeIndex 创建DataFrame.set_index-上采样和下采样的解决方案:

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:
df = Transit.resample('D', on='Timestamp').mean()
print (df)
Gate
Timestamp
2013-11-01 62

编辑:要删除所有重复 Timestamp的行,请将参数 subset添加到 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()

关于python-3.x - 仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但获得了 'Int64Index'的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55474088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com