如何将秒重新取至毫秒,然后在Python中插值

发布于 2025-02-07 00:49:29 字数 1053 浏览 1 评论 0原文

我有以下第二分辨率数据框架:

                     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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2025-02-14 00:49:29

recample默认情况下使用dataframe的索引,因此您可以:

  • 通过df = df = df.set_index('Timestamp'),然后重新示例。

  • 选择通过df.Resample('MS',on ='Timestamp')

亲自使用DateTime索引,因为它有一些好处:

df.set_index('timestamp', inplace=True)
df.resample('ms').interpolate('linear')

...

df.resample('ms').interpolate('linear')
                                   value
2015-02-21 03:42:35+00:00         45.000
2015-02-21 03:42:35.001000+00:00  45.001
2015-02-21 03:42:35.002000+00:00  45.002
2015-02-21 03:42:35.003000+00:00  45.003
2015-02-21 03:42:35.004000+00:00  45.004
...                                  ...
2015-02-21 03:42:39.996000+00:00  54.020
2015-02-21 03:42:39.997000+00:00  54.015
2015-02-21 03:42:39.998000+00:00  54.010
2015-02-21 03:42:39.999000+00:00  54.005
2015-02-21 03:42:40+00:00         54.000

[5001 rows x 1 columns]

您也可以指定如果您不希望如此高的分辨率:

df.resample('50ms').interpolate('linear')

...

                                  value
2015-02-21 03:42:35+00:00         45.00
2015-02-21 03:42:35.050000+00:00  45.05
2015-02-21 03:42:35.100000+00:00  45.10
2015-02-21 03:42:35.150000+00:00  45.15
2015-02-21 03:42:35.200000+00:00  45.20
...                                 ...
2015-02-21 03:42:39.800000+00:00  55.00
2015-02-21 03:42:39.850000+00:00  54.75
2015-02-21 03:42:39.900000+00:00  54.50
2015-02-21 03:42:39.950000+00:00  54.25
2015-02-21 03:42:40+00:00         54.00

[101 rows x 1 columns]

resample by default uses the index of your dataframe, so you can either:

  • Make this column the index via df = df.set_index('timestamp') and then resample.

or

  • Choose to resample on that column via df.resample('ms', on='timestamp')

Personally, I'd use a datetime index as there are some benefits to it:

df.set_index('timestamp', inplace=True)
df.resample('ms').interpolate('linear')

...

df.resample('ms').interpolate('linear')
                                   value
2015-02-21 03:42:35+00:00         45.000
2015-02-21 03:42:35.001000+00:00  45.001
2015-02-21 03:42:35.002000+00:00  45.002
2015-02-21 03:42:35.003000+00:00  45.003
2015-02-21 03:42:35.004000+00:00  45.004
...                                  ...
2015-02-21 03:42:39.996000+00:00  54.020
2015-02-21 03:42:39.997000+00:00  54.015
2015-02-21 03:42:39.998000+00:00  54.010
2015-02-21 03:42:39.999000+00:00  54.005
2015-02-21 03:42:40+00:00         54.000

[5001 rows x 1 columns]

You can also specify how many ms, if you don't want quite such a high resolution:

df.resample('50ms').interpolate('linear')

...

                                  value
2015-02-21 03:42:35+00:00         45.00
2015-02-21 03:42:35.050000+00:00  45.05
2015-02-21 03:42:35.100000+00:00  45.10
2015-02-21 03:42:35.150000+00:00  45.15
2015-02-21 03:42:35.200000+00:00  45.20
...                                 ...
2015-02-21 03:42:39.800000+00:00  55.00
2015-02-21 03:42:39.850000+00:00  54.75
2015-02-21 03:42:39.900000+00:00  54.50
2015-02-21 03:42:39.950000+00:00  54.25
2015-02-21 03:42:40+00:00         54.00

[101 rows x 1 columns]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文