重采样:指的是将时间序列从一个频率转化为另一个频率进行处理的过程,将高频率数据转化为低频率数据为降采样,低频率转化为高频率为升采样
demo.py(重采用,resample(),根据不同时间频率进行采样(分组)然后再聚合统计):
# coding=utf-8
import numpy as np
import pandas as pd
date_index = pd.date_range(start="20190101", periods=100, freq="D")
# 将时间序列作为索引构建DataFrame
df = pd.DataFrame(np.random.uniform(10, 50,(100,1)), index=date_index)
print(df)
'''
0
2019-01-01 23.480251
2019-01-02 37.804496
2019-01-03 45.707225
2019-01-04 33.236095
... ...
2019-04-07 24.725458
2019-04-08 37.328022
2019-04-09 27.507839
2019-04-10 44.131724
'''
# resample()重采样 (根据指定时间频率进行(分组)统计)
df2 = df.resample("M") # "M"表示每月最后一天
print(df2) # DatetimeIndexResampler [freq=<MonthEnd>, axis=0, closed=right, label=right, convention=start, base=0]
print(df2.mean()) # mean()表示求均值
'''
0
2019-01-31 28.166975
2019-02-28 27.531518
2019-03-31 29.147531
2019-04-30 36.036054
'''
print(df.resample("10D").mean()) # resample()重采样 "10D"表示10天
'''
0
2019-01-01 26.968151
2019-01-11 28.782230
2019-01-21 30.097632
2019-01-31 22.845797
2019-02-10 32.542558
2019-02-20 26.453604
2019-03-02 32.793139
2019-03-12 26.577123
2019-03-22 27.702987
2019-04-01 36.036054
'''