Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了';Int64Index';_Python 3.x_Pandas_Datetime_Dataframe_Resampling - Fatal编程技术网

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