繁体   English   中英

Python Pandas,使用 pct_change 函数重采样数据

[英]Python Pandas, Resampling data with pct_change function

我在使用非标准函数重新采样我的数据时遇到了问题。 m 数据的头部现在看起来像这样:

Time
2009-01-30 09:30:00    84.9800
2009-01-30 09:39:00    85.0800
2009-01-30 09:40:00    84.9350
2009-01-30 09:45:00    84.8200
2009-01-30 09:48:00    84.9900
2009-01-30 09:55:00    84.6800
2009-01-30 09:56:00    84.7700
2009-01-30 09:59:00    84.2800
2009-01-30 10:00:00    84.2400
2009-01-30 10:06:00    84.1500
2009-01-30 10:09:00    84.2404
2009-01-30 10:10:00    84.1500
2009-01-30 10:11:00    83.9400
2009-01-30 10:15:00    83.8550
2009-01-30 10:16:00    83.9500
2009-01-30 10:24:00    83.9300
2009-01-30 10:25:00    83.9400
2009-01-30 10:26:00    83.9300
2009-01-30 10:29:00    83.7200
2009-01-30 10:31:00    83.5300
2009-01-30 10:32:00    83.4400
2009-01-30 10:33:00    83.4400
2009-01-30 10:34:00    83.4800
2009-01-30 10:35:00    83.4400
2009-01-30 10:36:00    83.5100
2009-01-30 10:44:00    83.6200
2009-01-30 10:45:00    83.6400
2009-01-30 10:46:00    83.6300
2009-01-30 10:48:00    83.5500
2009-01-30 10:49:00    83.5200

Name: spyo, dtype: float64

我想使用每小时时间范围重新采样数据,并且应该返回 10:30 和 9:30 之间的值的百分比变化,然后在 11:30 和 10:30 之间等。

Data.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 964454 entries, 2009-01-30 09:30:00 to 2016-03-01 09:33:00
Data columns (total 6 columns):
spyo    964454 non-null float64
spyc    964454 non-null float64
spyv    964454 non-null float64
vxxo    964454 non-null float64
vxxc    964454 non-null float64
vxxv    964454 non-null int64
dtypes: float64(5), int64(1)

在 Pandas 0.18 或更新版本中,您可以使用Series.resample

def percent_change(x):
    if len(x):
        return (x[-1]-x[0])/x[0]

ser.resample('60T', base=30).apply(percent_change)

这产生

Time
2009-01-30 09:30:00   -0.014827
2009-01-30 10:30:00   -0.000120
Freq: 60T, Name: spyo, dtype: float64

如果没有base=30ser.resample('60T')会将系列重新采样为 60 分钟的时间间隔(分钟和秒等于 0)。 base=30 ,60 分钟的间隔将移动 30 分钟。 因此, Times显示9:3010:30而不是9:0010:00

第一行显示从9:3010:30的百分比变化。 第二行,从10:30ser的最后一次10:49

apply方法允许您使用 custum 函数聚合 60 分钟的时间间隔。 文档的最底部,您会找到另一个resample/apply示例。

在 Pandas 0.17 或更早版本中, 语法有点不同,但思想是相同的:

ser.resample('60T', base=30, how=percent_change)

例如,

import numpy as np
import pandas as pd
np.random.seed(2016)

N = 100
index = ((pd.date_range('2009-01-01', periods=N//2, freq='2T'))
         .union(pd.date_range('2009-01-01 4:00', periods=N//2, freq='2T')))
Data = pd.DataFrame(np.random.random((N,5)), 
                    columns='spyo spyc spyv vxxo vxxc'.split(),
                    index=index)
Data['vxxv'] = np.random.randint(10, size=(N,))

def percent_change(x):
    if len(x):
        return (x[-1]-x[0])/x[0]
print(Data.resample('60T', base=30).apply(percent_change))

产量

                          spyo      spyc      spyv      vxxo      vxxc  \
2008-12-31 23:30:00  -0.290145  0.116518 -0.767117  0.019722 -0.329499   
2009-01-01 00:30:00   0.957057  0.113174  0.331076 -0.179291  0.397392   
2009-01-01 01:30:00   0.412948 -0.366011  0.092585  0.455002  2.637628   
2009-01-01 02:30:00        NaN       NaN       NaN       NaN       NaN   
2009-01-01 03:30:00   0.169505 -0.901438  1.287304  8.042780 -0.189155   
2009-01-01 04:30:00  40.559281 -0.510897  0.316828  0.064967  0.236498   
2009-01-01 05:30:00   0.009669 -0.232149  2.055451 -0.210185  0.516835   

                         vxxv  
2008-12-31 23:30:00  7.000000  
2009-01-01 00:30:00  0.000000  
2009-01-01 01:30:00 -0.333333  
2009-01-01 02:30:00       NaN  
2009-01-01 03:30:00  2.500000  
2009-01-01 04:30:00  4.000000  
2009-01-01 05:30:00 -0.333333  

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM