【Pandas】解析resample函数中重采样频率‘freq‘用法(附参数说明表)
"""pandas案例中的一行代码"""
crime.resample('10AS').sum()
Pandas中的resample,重采样,是对原样本重新处理的一个方法,是一个对常规时间序列数据重新采样和频率转换的便捷的方法。正是因为这行代码中的'10AS'
使我萌生了想要弄懂这个函数的想法!
1 参数介绍
DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start',kind=None, loffset=None, limit=None, base=0)
参数 | 说明 |
---|---|
freq | 表示重采样频率,例如‘M’、‘5min’,Second(15) |
how=‘mean’ | 用于产生聚合值的函数名或数组函数,例如‘mean’、‘ohlc’、np.max等,默认是‘mean’,其他常用的值由:‘first’、‘last’、‘median’、‘max’、‘min’ |
axis=0 | 默认是纵轴,横轴设置axis=1 |
fill_method = None | 升采样时如何插值,比如‘ffill’、‘bfill’等 |
closed = ‘right’ | 在降采样时,各时间段的哪一段是闭合的,‘right’或‘left’,默认‘right’ |
label= ‘right’ | 在降采样时,如何设置聚合值的标签,例如,9:30-9:35会被标记成9:30还是9:35,默认9:35 |
loffset = None | 面元标签的时间校正值,比如‘-1s’或Second(-1)用于将聚合标签调早1秒 |
limit=None | 在向前或向后填充时,允许填充的最大时期数 |
kind = None | 聚合到时期(‘period’)或时间戳(‘timestamp’),默认聚合到时间序列的索引类型 |
convention = None | 当重采样时期时,将低频率转换到高频率所采用的约定(start或end)。默认‘end’ |
2 采样频率
这个参数是目前使用频率最高的,所以整理下来供查阅。
example:
index = pd.date_range('1/1/2000', periods=9, freq='T')
series = pd.Series(range(9), index=index)
series
#Output
2000-01-01 00:00:00 0
2000-01-01 00:01:00 1
2000-01-01 00:02:00 2
2000-01-01 00:03:00 3
2000-01-01 00:04:00 4
2000-01-01 00:05:00 5
2000-01-01 00:06:00 6
2000-01-01 00:07:00 7
2000-01-01 00:08:00 8
Freq: T, dtype: int64
rule 参数如下表 :
参数 | 含义 |
---|---|
B | business day |
C | custom business day (experimental) |
D | calendar day |
W | weekly |
M | month end |
BM | business month end |
CBM | custom business month end |
MS | month start |
BMS | business month start |
CBMS | custom business month start |
Q | quarter end |
BQ | business quarter end |
QZ | quarter start |
BS | business quarter start |
A,Y | year end frequency |
BA,BY | business year end |
AS,YS | year start |
BAS,BYS | business year start |
BH | business hour |
H | hourly |
T,min | minutely |
S | secondly |
L | milliseconds |
M,ms | microseconds |
N | nanoseconds |
相关链接:
Pandas中resample方法详解_python_脚本之家 (jb51.net)
pandas.DataFrame.resample — pandas 1.3.0 documentation (pydata.org)
pandas resample重采样频率介绍 、附案例_code_zbw-CSDN博客
python – What values are valid in Pandas ‘Freq’ tags? – Stack Overflow
来源:火柴先生