简体   繁体   中英

Pandas dataframe.resample TypeError 'Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

I have a time-series dataframe df that I want to resample so that I can plot it against another dataframe with different sampling frequency. The times start off as Epoch time so I convert them to datetime:

>>> df
                 x         y
0     1.541376e+09  0.044084
1     1.541376e+09  0.044309
2     1.541376e+09  0.044772
3     1.541376e+09  0.044320
4     1.541376e+09  0.046859
...            ...       ...
4122  1.541462e+09  0.278618
4123  1.541462e+09  0.276922
4124  1.541462e+09  0.274893
4125  1.541462e+09  0.276190
4126  1.541462e+09  0.273271

[4127 rows x 2 columns]

df.x = df.x.apply(datetime.datetime.fromtimestamp)
>>> df
                           x         y
0    2018-11-05 13:00:21.884  0.044084
1    2018-11-05 13:00:44.581  0.044309
2    2018-11-05 13:01:07.276  0.044772
3    2018-11-05 13:01:29.973  0.044320
4    2018-11-05 13:01:52.670  0.046859
...                      ...       ...
4122 2018-11-06 12:58:11.260  0.278618
4123 2018-11-06 12:58:33.955  0.276922
4124 2018-11-06 12:58:56.652  0.274893
4125 2018-11-06 12:59:19.349  0.276190
4126 2018-11-06 12:59:42.046  0.273271

[4127 rows x 2 columns]

Then I try use df.resample, and I get TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'.

Looking at the type of df.x[o], it is not datetime.datetime but pandas._libs.tslibs.timestamps.Timestamp, which looks like its meant to be equivalent? Anyways if someone could tell me how to get this resample() to work, I would appreciate it.

Ok, it looks like if I set the index as Ynjxsjmh suggested (making sure it is actually done in-place...), then resample() work. But it returns some werid object, and then I have to do.sum() on it, which returns the dataframe that I want.

Annoyingly, my two dataframes still ended up with different lengths by 1 row, but I just dropped the final row of one of them with df.drop(df.tail(1).index, inplace = True) and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM