gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:35 24 4
gpt4 key购买 nike

我可能在使用非标准函数重新采样数据时遇到问题。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 分钟间隔。最底部the docs您会发现另一个resample/apply示例。

在 Pandas 0.17 或更高版本中,the syntax is a bit different但想法是一样的:

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

关于Python Pandas,使用 pct_change 函数重新采样数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com