Pandas 数据框:使用线性插值重新采样

Pandas data frame: resample with linear interpolation(Pandas 数据框:使用线性插值重新采样)
本文介绍了Pandas 数据框:使用线性插值重新采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用一种相当基本的重采样方法来处理 pandas 数据框.我的数据框 df 由日期时间条目索引并包含价格

I am trying to get a fairly basic resampling method to work with a pandas data frame. My data frame df is indexed by datetime entries and contains prices

                               price
datetime                            
2000-08-16 09:29:55.755000  7.302786
2000-08-16 09:30:10.642000  7.304059
2000-08-16 09:30:26.598000  7.304435
2000-08-16 09:30:41.372000  7.304314
2000-08-16 09:30:56.718000  7.304334

我想将其缩减为 5 分钟.使用

I would like to downsample this to 5min. Using

df.resample(rule='5Min',how='last',closed='left')

在我的数据中以 5 分钟的倍数取最左边的点;类似

takes the closest point to the left in my data of a multiple of 5min; similarly

df.resample(rule='5Min',how='first',closed='left')

将关闭点移到右侧.但是,我想采用左右点之间的线性插值,例如如果我的 df 包含两个连续的条目

takes the closes point to the right. However, I would like to take the linear interpolation between the point to the left and right instead, e.g. if my df contains the two consecutive entries

time t1, price p1
time t2, price p2

t1<t<t2 where t is a multiple of 5min

那么重新采样的数据框应该有条目

then the resampled dataframe should have the entry

time t, price p1+(t-t1)/(t2-t1)*(p2-p1)

推荐答案

尝试创建两个单独的数据框,reset_index 它们(因此它们具有相同的数字索引),fillna在他们身上,然后对 df1 和 df2 进行数学运算.例如:

try creating two separate dataframes, reset_index them (so they have the same numerical index), fillna on them, and then just do the math on df1 and df2. e.g:

df1 = df.resample(rule='5Min',how='last',closed='left').reset_index().fillna(method='ffill')
df2 = df.resample(rule='5Min',how='first',closed='left').reset_index().fillna(method='ffill')

dt = df1.datetime - df2.datetime
px_fld = df1.price + ...

类似的东西应该可以解决问题.

something like that should do the trick.

这篇关于Pandas 数据框:使用线性插值重新采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)