[PYTHON] pandas resample and rolling

Introduction

I'll check pandas'resample and rolling every time because they look similar and not similar, so I'll summarize them briefly.

reference

resample

Function body

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
)

Subsequent functions

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 (NaNFill in)
ffill forward fill,The value closest to the past direction (NaNFill 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

rolling

Function body

DataFrame.rolling(
    window,
    min_periods=None,
    freq=None,
    center=False,
    win_type=None,
    on=None,
    axis=0,
    closed=None
)

Subsequent functions

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

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

pandas resample and rolling
jupyter and pandas installation
pandas index and reindex
Pandas averaging and listing
Correspondence between pandas and SQL
Key additions to pandas 1.1.0 and 1.0.0
How to use Pandas Rolling
Behavior of pandas rolling () method
Pandas
Precautions when using codecs and pandas
[Pandas] Find quartiles and detect outliers
Ignore # line and read in pandas
[Python] What is pandas Series and DataFrame?
Import of japandas with pandas 1.0 and above
A little scrutiny of pandas 1.0 and dask
[pandas] .csv file reading and display method
Grouping csv and getting minimum value (pandas)
Load csv with pandas and play with Index
How to use pandas Timestamp and date_range
Read CSV and analyze with Pandas and Seaborn