我遇到的问题可能是使用非标准功能重新采样我的数据。 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)
答案 0 :(得分: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=30
,ser.resample('60T')
会将系列重新采样为60分钟的间隔(分钟和秒等于0)。使用base=30
时,60分钟的间隔会移动30分钟。因此,Times
显示9:30
和10:30
而不是9:00
和10:00
。
第一行显示从9:30
到10:30
的百分比变化。第二行,从10:30
到最后一次ser
,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