简体   繁体   English

Pandas dataframe.resample TypeError '仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“RangeIndex”实例

[英]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.我有一个时间序列 dataframe df 我想重新采样,以便我可以 plot 它与另一个具有不同采样频率的 dataframe 对比。 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'.然后我尝试使用 df.resample,我得到 TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了一个“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?查看 df.x[o] 的类型,它不是 datetime.datetime 而是 pandas._libs.tslibs.timestamps.Timestamp,看起来它的意思是等价的? Anyways if someone could tell me how to get this resample() to work, I would appreciate it.无论如何,如果有人能告诉我如何让这个 resample() 工作,我将不胜感激。

Ok, it looks like if I set the index as Ynjxsjmh suggested (making sure it is actually done in-place...), then resample() work.好的,看起来如果我按照 Ynjxsjmh 的建议设置索引(确保它实际上是就地完成的......),然后 resample() 工作。 But it returns some werid object, and then I have to do.sum() on it, which returns the dataframe that I want.但它会返回一些奇怪的 object,然后我必须对其进行 do.sum(),它会返回我想要的 dataframe。

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.令人讨厌的是,我的两个数据帧的长度仍然相差 1 行,但我只是用 df.drop(df.tail(1).index, inplace = True) 删除了其中一个数据帧的最后一行,它就起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' Pandas重采样:TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到'RangeIndex'的实例 - Pandas Resampling: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例,我不知道为什么 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' and I can't figure out why Pandas TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有“ Int64Index”的实例 - Pandas TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index' 类型错误:如何修复仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“Index”的实例 - TypeError: How to fix Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index' Python datetime 仍然给出“TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'” - Python datetime still gives "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'" Pandas 重采样错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效 - Pandas Resampling error: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex pandas 不能按日期分组,仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但 - pandas cannot group by date, Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but TypeError:仅当dtype为datetime64 [ns]时对DatetimeIndex,TimedeltaIndex或PeriodIndex有效 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex when dtype is datetime64[ns] pandas Sqlite 和重采样错误仅对 DatetimeIndex 有效 - pandas Sqlite and resample error Only valid with DatetimeIndex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM