Python 3.x 仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了';Int64Index';
我正在尝试对此数据帧的时间戳列重新采样:Python 3.x 仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了';Int64Index';,python-3.x,pandas,datetime,dataframe,resampling,Python 3.x,Pandas,Datetime,Dataframe,Resampling,我正在尝试对此数据帧的时间戳列重新采样: 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
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
by-上采样和下采样解决方案:
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
对于下采样,可以使用上的参数:
df = Transit.resample('D', on='Timestamp').mean()
print (df)
Gate
Timestamp
2013-11-01 62
编辑:用于删除具有重复时间戳的所有行
将参数子集添加到:
我已经试过了,我得到的是:不能用方法或方法重新索引非唯一索引limit@Dimi-已编辑的答案。事实上,我尝试删除重复项的方式是错误的,这解决了我的问题。@Dimi-如果没有参数子集
,如果通过检查所有列筛选出重复项,只需指定一列。当我运行Transit.set_index('Timestamp').resample('D').sum()
在我的例子中,输出如下:2013-11-01T11:00:00.000000000
,不再有列Timestamp
!
Transit.drop_duplicates(subset=['Timestamp'], inplace=True)
Transit.Timestamp = pd.to_datetime(Transit.Timestamp)
df = Transit.set_index('Timestamp').resample('1H').pad()