【发布时间】:2021-05-03 04:09:44
【问题描述】:
我有一个时间序列数据帧 df 我想重新采样,以便我可以将它与另一个具有不同采样频率的数据帧进行对比。时间以纪元时间开始,所以我将它们转换为日期时间:
>>> 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]
然后我尝试使用 df.resample,我得到 TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了一个“RangeIndex”实例。
查看df.x[o]的类型,不是datetime.datetime而是pandas._libs.tslibs.timestamps.Timestamp,看起来是等价的吗? 无论如何,如果有人能告诉我如何让这个 resample() 工作,我将不胜感激。
【问题讨论】:
-
df.set_index('x', inplace=True)
标签: python python-3.x pandas dataframe datetime