机器学习pandas之时间重采样笔记

 周期由高频率转向低频率称为降采样:例如5分钟股票交易数据转换为日交易数据

相反,周期也可以由低频转向高频称为升采样

其他重采样:例如每周三(W-WED)转换为每周五(W-FRI)

 1 import pandas as pd
 2 import numpy as np
 3 
 4 # 创建一个时间戳序列
 5 s = pd.Series(np.random.randn(5),
 6                      index=pd.date_range('2016-04-01',periods=5,freq='M'))
 7 # 注意它给的起始时间,与输出的时间对比,
 8 # 它给定的频率为月份输出的月份从每个月的最后一天算起
 9 # 输出
10 2016-04-30   -0.487238
11 2016-05-31    0.376708
12 2016-06-30   -1.830840
13 2016-07-31   -0.426218
14 2016-08-31    1.913151
15 Freq: M, dtype: float64
16 
17 # 将时间戳的序列转换为时期序列,
18 s.to_period()
19 # 输出
20 2016-04   -0.487238
21 2016-05    0.376708
22 2016-06   -1.830840
23 2016-07   -0.426218
24 2016-08    1.913151
25 Freq: M, dtype: float64
26 
27 # 创建周期频率为天的时间序列
28 ts = pd.Series(np.random.randn(5),
29                       index=pd.date_range('2016-12-29',periods=5,freq='D'))
30 # 这个时间序列与第5行的不同,它的频率变以天为单位
31 
32 # 当转换为时期序列,它的频率也是默认天为单位
33 ts.to_period() # 与28行的结果相同
34 
35 pts = ts.to_period(freq='M') 
36 # 把频率变为月时原来的总时间没变只是频率变了它将输出
37 2016-12   -0.525272
38 2016-12   -2.610914
39 2016-12    1.094692
40 2017-01   -1.721324
41 2017-01    0.631946
42 Freq: M, dtype: float64
43 
44 # 也可以再转为时间戳序列
45 pts.to_timestamp()  
46 # 输出
47 2016-12-01    0.797379
48 2016-12-01   -0.085046
49 2016-12-01   -0.271226
50 2017-01-01    1.320668
51 2017-01-01    0.168546
52 dtype: float64
53 
54 pts.to_timestamp(how='end') # 可以输出每月的最后结束时间
55 # 输出
56 2016-12-31 23:59:59.999999999    0.797379
57 2016-12-31 23:59:59.999999999   -0.085046
58 2016-12-31 23:59:59.999999999   -0.271226
59 2017-01-31 23:59:59.999999999    1.320668
60 2017-01-31 23:59:59.999999999    0.168546
61 dtype: float64
62 
63 # 创建以周期频率为分的时间序列
64 ts = pd.Series(np.random.randint(0,50,60),
65        index=pd.date_range('2016-04-25 09:30',periods=60,freq='T'))
66 # 通过降采样,降低时间频率
67 ts.resample('5min',how='sum')  # how='sum',表示对降采样的时间段求和
68 # 输出  它是以时间开始的时候为准,即时间轴的左端
69 2016-04-25 09:30:00    135
70 2016-04-25 09:35:00    120
71 2016-04-25 09:40:00    138
72 2016-04-25 09:45:00    101
73 ......
74 
75 ts.resample('5min',how='sum',label='right')  # 也可以以末尾时间为准,即时间轴右端
76 # 输出
77 2016-04-25 09:35:00    135
78 2016-04-25 09:40:00    120
79 2016-04-25 09:45:00    138
80 2016-04-25 09:50:00    101
81 ......
82 
83 ts.resample('5min',how='ohlc')
84 # 它创建了一个DataFrame,以时间为行索引,分别以open、high、low、close
85 # 为列索引,how='ohlc'就是前面每个列索引的首字母
86 
87 ts = pd.Series(np.random.randint(0,50,100),
88        index=pd.date_range('2016-03-01',periods=100,freq='D'))
89 
90 ts.groupby(lambda x: x.month).sum()
91 # lambda表达式中的x为ts,把ts中的月份进行分组并求每月的总量
92 ts.groupby(ts.index.to_period('M')).sum()  # 与上一行效果相同
 1 import pandas as pd
 2 import numpy as np
 3 
 4 df = pd.DataFrame(np.random.randint(1,50,2),
 5         index=pd.date_range('2016-04-22',periods=2,freq='W-FRI'))
 6 # 它的周期频率为星期五
 7 
 8 df.resample('D',fill_method='ffill')  
 9 # 用升采样的方式,将频率提高的每天,
10 # fill_method='ffill',表示向上填充值,即所要填充的值与上一行相同
11 
12 df.resample('D',fill_method='ffill',limit=3)
13 # limit=3表示最后3行被限制不填入值,默认填NaN
14 
15 df.resample('W-MON',fill_method='ffill')
16 # 表示以以周一为频率重新采样
17 
18 
19 df = pd.DataFrame(np.random.randint(2,30,(24,4)),
20                  index=pd.period_range('2015-01','2016-12',freq='M'),
21                  columns=list('ABCD')
22 
23 df.resample('A-DEC',how='sum')  # 用年频率重采样
24 
25 df.resample('A-MAR',how='sum')  # 用财年重采样,每年的三月分
26 
27 pdf = df.resample('A-DEC',how='mean')  # 用年频率重采样,算出每年的均值
28 
29 pdf.resample('Q-DEC',fill_method='ffill')  # 将上一行的值再以季度频率重采样

猜你喜欢

转载自www.cnblogs.com/yang901112/p/11434305.html