简体   繁体   English

熊猫定制重新采样时间序列数据

[英]Pandas custom re-sample for time series data

I have a time series data in 1 Min frequency. 我有1分钟频率的时间序列数据。 I would like re-sample the data for every 5 min and re-sample data should include the data of first time step, middle time step and last time step. 我想每5分钟重新采样一次数据,并且重新采样的数据应包括第一时间步长,中间时间步长和最后时间步长的数据。

I have tried like this, but I am not getting what I am expecting... 我已经尝试过这种方法,但是却没有达到我的期望...

def my_fun(array)
     return array[0],array[-1]


df=pd.DataFrame(np.arange(60),index=pd.date_range('2017-01-01 00:00','2017-01-01 00:59', freq='1T'

df.resample('5T').apply(my_fun)

If I understood you correctly then you want the data for minutes 0,2,4,5,7,9,10,... in a new dataframe. 如果我对您的理解正确,那么您希望在新数据框中记录分钟0,2,4,5,7,9,10,...的数据。 A faster way than using resample may be: 比使用重采样更快的方法可能是:

df=pd.DataFrame(np.arange(60),index=pd.date_range('2017-01-01 00:00','2017-01-01 00:59', freq='1T'))
l = len(df)
df.loc[df.iloc[range(2,l,5)].index | df.iloc[range(4,l,5)].index | df.iloc[range(0,l,5)].index]

Output: 输出:

                        0
2017-01-01 00:00:00     0
2017-01-01 00:02:00     2
2017-01-01 00:04:00     4
2017-01-01 00:05:00     5
2017-01-01 00:07:00     7
2017-01-01 00:09:00     9
2017-01-01 00:10:00     10

If you just wanted a combined list of your selected data in one row then you were almost there: 如果您只想将所选数据的组合列表排成一行,那么您就快到了:

def my_fun(array):
      return [array[0], array[2], array[4]]

df=pd.DataFrame({'0':np.arange(60)}, index=pd.date_range('2017-01-01 00:00','2017-01-01 00:59', freq='1T'))
df.resample('5T').apply(my_fun)

Output: 输出:

                        0
2017-01-01 00:00:00     (0, 2, 4)
2017-01-01 00:05:00     (5, 7, 9)
2017-01-01 00:10:00     (10, 12, 14)

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

相关问题 将 df 重新采样到微秒 - Pandas - Re-sample df to microsecond - Pandas 如何设置5分钟滚动平均值窗口以使用Pandas Python重新采样数据 - How to set the 5 minutes rolling mean window to re-sample data with Pandas Python 在python pandas中,如何重新采样和插入DataFrame? - In python pandas, how can I re-sample and interpolate a DataFrame? 如何在 python 中重新采样和插入新数据 - How to re-sample and interpolate new data in python 在 Pandas 中,如果我们通过平均将 1 分钟间隔数据重新采样为 15 分钟间隔,我们可以选择如何重新采样和分配数据 - In pandas If we are re-sampling a 1-minute interval data to a 15-minute interval by averaging can we select how to re-sample and assiggn the data python在统一的半年期重新采样(在熊猫重新采样中等于“ BQ”) - python re-sample at a uniform semiannual period (equivaent of 'BQ' in pandas resample) 带有多索引的熊猫数据帧重新采样时间序列索引 - Panda dataframe re-sample timeseries index with multiindex 熊猫时间序列索引—重新 - Pandas time series indexing — re 合并并采样两个熊猫时间序列 - merge and sample two pandas time series Pandas:在给定时间(例如每一天)对插值时间序列数据进行采样的更简单方法 - Pandas: easier way to sample interpolated time series data at given times (e.g. every full day)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM