import pandas as pd
import numpy as np
rng = pd.date_range('1/1/2000',periods=100,freq='D')
ts = pd.Series(np.random.randn(len(rng)),index=rng)
ts.resample('M').mean() # 将100天按月进行降采样(聚合)
2000-01-31 -0.156092
2000-02-29 0.060607
2000-03-31 -0.039608
2000-04-30 -0.154838
Freq: M, dtype: float64
ts.resample('M',kind='period').mean()
2000-01 -0.156092
2000-02 0.060607
2000-03 -0.039608
2000-04 -0.154838
Freq: M, dtype: float64
rng = pd.date_range('1/1/2000',periods=12,freq='T')
ts = pd.Series(np.arange(12),index=rng)
ts
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
2000-01-01 00:09:00 9
2000-01-01 00:10:00 10
2000-01-01 00:11:00 11
Freq: T, dtype: int32
ts.resample('5min').sum()
2000-01-01 00:00:00 10
2000-01-01 00:05:00 35
2000-01-01 00:10:00 21
Freq: 5T, dtype: int32
ts.resample('5min',closed='right').sum()
1999-12-31 23:55:00 0
2000-01-01 00:00:00 15
2000-01-01 00:05:00 40
2000-01-01 00:10:00 11
Freq: 5T, dtype: int32
ts.resample('5min',closed='right',label='right').sum()
2000-01-01 00:00:00 0
2000-01-01 00:05:00 15
2000-01-01 00:10:00 40
2000-01-01 00:15:00 11
Freq: 5T, dtype: int32
ts.resample('5min',closed='right',loffset='-1s').sum()
1999-12-31 23:54:59 0
1999-12-31 23:59:59 15
2000-01-01 00:04:59 40
2000-01-01 00:09:59 11
Freq: 5T, dtype: int32
在金融领域常用的聚合方式–OHLC,它会计算各个面元的:第一个值(开盘)、最后一个值(收盘)、最大值和最小值,并产生一个DataFrame
print(ts.resample('5min').ohlc())
open high low close
2000-01-01 00:00:00 0 4 0 4
2000-01-01 00:05:00 5 9 5 9
2000-01-01 00:10:00 10 11 10 11
rng = pd.date_range('1/1/2000',periods=100,freq='D')
ts = pd.Series(np.arange(100),index=rng)
ts.groupby(lambda x:x.month).mean() # 等价于 ts.groupby(rng.month).mean()
1 15
2 45
3 75
4 95
dtype: int32
ts.groupby(lambda x:x.weekday).mean() # 按周聚合
0 47.5
1 48.5
2 49.5
3 50.5
4 51.5
5 49.0
6 50.0
dtype: float64
frame = pd.DataFrame(np.random.randn(2,4),
index = pd.date_range('1/1/2000',periods=2,freq='W-WED'),
columns = ['Colorado','Texas','New York','Ohio'])
print(frame)
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
df_daily = frame.resample('D')
print(df_daily.ffill())
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-06 -0.078765 1.389417 0.732726 0.816723
2000-01-07 -0.078765 1.389417 0.732726 0.816723
2000-01-08 -0.078765 1.389417 0.732726 0.816723
2000-01-09 -0.078765 1.389417 0.732726 0.816723
2000-01-10 -0.078765 1.389417 0.732726 0.816723
2000-01-11 -0.078765 1.389417 0.732726 0.816723
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
print(df_daily.ffill(limit=2))
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-06 -0.078765 1.389417 0.732726 0.816723
2000-01-07 -0.078765 1.389417 0.732726 0.816723
2000-01-08 NaN NaN NaN NaN
2000-01-09 NaN NaN NaN NaN
2000-01-10 NaN NaN NaN NaN
2000-01-11 NaN NaN NaN NaN
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
print(frame)
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
print(frame.resample('W-THU').ffill()) # 重采样后的结果开始为全NaN,使用ffill会使用2000-01-05和2000-01-12的值向前填充
Colorado Texas New York Ohio
2000-01-06 -0.078765 1.389417 0.732726 0.816723
2000-01-13 -0.663686 0.744384 1.395332 -0.031715
frame = pd.DataFrame(np.random.randn(24,4),
index = pd.period_range('1-2000','12-2001',freq='M'),
columns = ['Colorado','Texas','New York','Ohio'])
print(frame[:5])
Colorado Texas New York Ohio
2000-01 -1.956495 -0.689508 0.057439 -0.655832
2000-02 -0.491443 -1.731887 1.336801 0.659877
2000-03 -0.139601 -1.310386 -0.299205 1.194269
2000-04 0.431474 -1.312518 1.880223 0.379421
2000-05 -0.674796 0.471018 0.132998 0.509761
annual_frame = frame.resample('A-DEC').mean()
print(annual_frame)
Colorado Texas New York Ohio
2000 -0.332076 -0.762599 0.046917 0.224908
2001 -0.152922 0.168667 -0.326439 -0.052034
# Q-DEC:以12月做为最后一个季度的最后一个月进行升采样.也就是1-3月是1季度,4-6月是2季度,7-9月是3季度,10-12月是4季度
print(annual_frame.resample('Q-DEC').ffill())
Colorado Texas New York Ohio
2000Q1 -0.332076 -0.762599 0.046917 0.224908
2000Q2 -0.332076 -0.762599 0.046917 0.224908
2000Q3 -0.332076 -0.762599 0.046917 0.224908
2000Q4 -0.332076 -0.762599 0.046917 0.224908
2001Q1 -0.152922 0.168667 -0.326439 -0.052034
2001Q2 -0.152922 0.168667 -0.326439 -0.052034
2001Q3 -0.152922 0.168667 -0.326439 -0.052034
2001Q4 -0.152922 0.168667 -0.326439 -0.052034
# 使用2000Q4替换2000、2001Q4替换2001,这两个值2000Q4和2001Q4之间就是升采样新增的值
print(annual_frame.resample('Q-DEC',convention='end').ffill())
Colorado Texas New York Ohio
2000Q4 -0.332076 -0.762599 0.046917 0.224908
2001Q1 -0.332076 -0.762599 0.046917 0.224908
2001Q2 -0.332076 -0.762599 0.046917 0.224908
2001Q3 -0.332076 -0.762599 0.046917 0.224908
2001Q4 -0.152922 0.168667 -0.326439 -0.052034
print(annual_frame.resample('Q-MAR',convention='end').ffill())
Colorado Texas New York Ohio
2001Q3 -0.332076 -0.762599 0.046917 0.224908
2001Q4 -0.332076 -0.762599 0.046917 0.224908
2002Q1 -0.332076 -0.762599 0.046917 0.224908
2002Q2 -0.332076 -0.762599 0.046917 0.224908
2002Q3 -0.152922 0.168667 -0.326439 -0.052034