I'll check pandas'resample
and rolling
every time because they look similar and not similar, so I'll summarize them briefly.
pandas v0.20.3
DataFrame.resample(
rule,
how=None,
axis=0,
fill_method=None,
closed=None,
label=None,
convention='start',
kind=None,
loffset=None,
limit=None,
base=0,
on=None,
level=None
)
function | Description |
---|---|
first | The value closest to the future |
last | The value closest to the past direction |
bfill | backward fill,The value closest to the future (NaN Fill in) |
ffill | forward fill,The value closest to the past direction (NaN Fill in) |
count | Number of values |
unique | Number of unique values |
max | Maximum value |
min | minimum value |
mean | Average value |
median | Median |
sum | Total value |
var | Distributed |
std | standard deviation |
ohlc | Open price(opning), Highest price(highest),lowest price(lowest),closing price(closing) |
pad | = ffill |
DataFrame.rolling(
window,
min_periods=None,
freq=None,
center=False,
win_type=None,
on=None,
axis=0,
closed=None
)
function | Description |
---|---|
count | Number of values |
max | Maximum value |
min | minimum value |
sum | Total value |
mean | Average value |
median | Median |
var | Distributed |
std | standard deviation |
cov | Covariance matrix |
corr | Correlation matrix |
skew | skewness(Third moment) |
kurt | kurtosis(Fourth moment) |
quantile | Quantile |
apply | Aggregation by original function |
You can do your own aggregation with rolling (). Apply ()
--Example) FIR filter, moving average filter
import numpy as np
#Filter coefficient
b = np.ones(5) / 5
def f(x):
#x is an array of values in the window
# x[0]Is the oldest, x[-1]Is the newest value
#Return the aggregated value
return np.sum(b*x)
#Application
series.rolling(5, center=True).apply(f)
# series.rolling(5, center=True).apply(lambda x : np.sum(b*x))But OK
Recommended Posts