如何将秒重新取至毫秒,然后在Python中插值
我有以下第二分辨率数据框架:
timestamp value
------------------------------------------
0 2015-02-21 03:42:35+00:00 45
1 2015-02-21 03:42:36+00:00 46
2 2015-02-21 03:42:37+00:00 49
3 2015-02-21 03:42:38+00:00 55
4 2015-02-21 03:42:39+00:00 59
5 2015-02-21 03:42:40+00:00 54
...
该数据框是在运行后创建的:
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)
我想做的是乘坐秒的分辨率时间戳,然后作为毫秒毫秒进行重新采样,然后填充那些带有插值(线性插值)值的新的毫秒的时间戳,因此,我将获得现在的毫秒分辨率数据的数据框架。 我尝试了以下代码:
upsampled = df.resample('ms')
interpolated = upsampled.interpolate(method='linear')
interpolated.head()
我会收到以下错误:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
从以下行中:
upsampled = df.resample('ms')
如何解决数据帧问题,以便可以正确处理,重新采样和插值?由于看来如何阅读我的时间表数据存在某种问题。而且,如果毫秒可能太高的解决方案而无法合理处理,那么我也只有几秒钟的分数也可以,只是一些更高的东西 - 分辨率比秒。
I have the following second-resolution dataframe:
timestamp value
------------------------------------------
0 2015-02-21 03:42:35+00:00 45
1 2015-02-21 03:42:36+00:00 46
2 2015-02-21 03:42:37+00:00 49
3 2015-02-21 03:42:38+00:00 55
4 2015-02-21 03:42:39+00:00 59
5 2015-02-21 03:42:40+00:00 54
...
This dataframe was created after running:
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)
What I want to do is take my seconds resolution timestamps, and then resample as milliseconds, and then fill in those new millisecond timestamps with interpolated (linear interpolation) values, so I will be left with a dataframe of now millisecond-resolution data.
I tried the following code:
upsampled = df.resample('ms')
interpolated = upsampled.interpolate(method='linear')
interpolated.head()
And I receive the following error:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
from this line:
upsampled = df.resample('ms')
How can I resolve my dataframe issue so that the timestamps can be properly handled, resampled, and interpolated? Since it appears there is some sort of issue with how my timeseries data was read in. Also, if milliseconds is perhaps too high of a resolution to reasonably process, I would be fine with just fractions of seconds too, just something a bit higher-resolution than seconds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
recample
默认情况下使用dataframe的索引,因此您可以:df = df = df.set_index('Timestamp')
,然后重新示例。或
df.Resample('MS',on ='Timestamp')
亲自使用DateTime索引,因为它有一些好处:
您也可以指定如果您不希望如此高的分辨率:
resample
by default uses the index of your dataframe, so you can either:df = df.set_index('timestamp')
and then resample.or
df.resample('ms', on='timestamp')
Personally, I'd use a datetime index as there are some benefits to it:
You can also specify how many
ms
, if you don't want quite such a high resolution: