Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

后端 未结 1 573
无人共我
无人共我 2021-01-20 00:50

I\'m trying to resample this Timestamp column of this Dataframe:

  Transit.head():

      Timestamp                            Plat         


        
相关标签:
1条回答
  • 2021-01-20 01:33

    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
    

    For downsampling is possible use parameter on:

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

    EDIT: 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()
    
    0 讨论(0)
提交回复
热议问题