I am having issues with probably resampling my data using non standard functions. Head of m data looks like this now:
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
I want to resample the data using and hourly timeframe and should return the percent change of the value between 10:30 and 9:30 then between 11:30 and 10:30 etc.
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)
In Pandas 0.18 or newer, you could use Series.resample
:
def percent_change(x):
if len(x):
return (x[-1]-x[0])/x[0]
ser.resample('60T', base=30).apply(percent_change)
which yields
Time
2009-01-30 09:30:00 -0.014827
2009-01-30 10:30:00 -0.000120
Freq: 60T, Name: spyo, dtype: float64
Without base=30
, ser.resample('60T')
would resample the Series into 60-minute intervals (with minutes and seconds equal to 0). With base=30
, the 60-minute intervals are shifted by 30 minutes. Hence the Times
show 9:30
and 10:30
instead of 9:00
and 10:00
.
The first row shows the percent change from 9:30
to 10:30
. The second row, from 10:30
to the last time in ser
, 10:49
.
The apply
method allows you to aggregate the 60-minute intervals using a custum function. At the very bottom of the docs you'll find another example of resample/apply
.
In Pandas 0.17 or older, the syntax is a bit different but the idea is the same:
ser.resample('60T', base=30, how=percent_change)
For example,
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))
yields
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.