/
Lec13_pandas_time_series.py
215 lines (151 loc) · 4.75 KB
/
Lec13_pandas_time_series.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# # Time Series
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# ## Date and Time Data Types and Tools
from dateutil.parser import parse
parse('2011-01-03')
parse('Jan 31, 1997 10:45 PM')
parse('6/12/2011', dayfirst=True)
parse('6/12/2011')
#%%
datestrs = ['2011-07-06 12:00:00', '2011-08-06 00:00:00']
pd.to_datetime(datestrs)
idx = pd.to_datetime(datestrs + [None])
idx
idx[2]
pd.isnull(idx)
# ## Time Series Basics
dates = pd.date_range('1/1/2000', periods=100, freq='W-WED')
long_df = pd.DataFrame(np.random.randn(100, 4),
index=dates,
columns=['Colorado', 'Texas',
'New York', 'Ohio'])
long_df.loc['5-2001']
# ## Date Ranges, Frequencies, and Shifting
resampler = long_df.resample('D')
# ### Generating Date Ranges
#%%
index = pd.date_range('2012-04-01', '2012-06-01')
index
pd.date_range(start='2012-04-01', periods=20)
pd.date_range(end='2012-06-01', periods=20)
# ### Frequencies and Date Offsets
#%%
pd.date_range('2000-01-01', '2000-01-03 23:59', freq='4h')
pd.date_range('2000-01-01', periods=10, freq='1h30min')
# #### Week of month dates
rng = pd.date_range('2012-01-01', '2012-09-01', freq='WOM-3FRI')
list(rng)
# ### Shifting (Leading and Lagging) Data
#%%
ts = pd.Series(np.random.randn(4),
index=pd.date_range('1/1/2000', periods=4, freq='M'))
ts
ts.shift(2)
ts.shift(-2)
# ## Periods and Period Arithmetic
#%%
p = pd.Period(2007, freq='A-DEC')
p
rng = pd.period_range('2000-01-01', '2000-06-30', freq='M')
rng
pd.Series(np.random.randn(6), index=rng)
values = ['2001Q3', '2002Q2', '2003Q1']
index = pd.PeriodIndex(values, freq='Q-DEC')
index
# ### Converting Timestamps to Periods (and Back)
rng = pd.date_range('2000-01-01', periods=3, freq='M')
ts = pd.Series(np.random.randn(3), index=rng)
ts
pts = ts.to_period()
pts
rng = pd.date_range('1/29/2000', periods=6, freq='D')
ts2 = pd.Series(np.random.randn(6), index=rng)
ts2
ts2.to_period('M')
# ### Creating a PeriodIndex from Arrays
#%%
data = pd.read_csv('examples/macrodata.csv')
data.head(5)
data.year
data.quarter
index = pd.PeriodIndex(year=data.year, quarter=data.quarter,
freq='Q-DEC')
index
data.index = index
data.infl
# ## Resampling and Frequency Conversion
#%%
rng = pd.date_range('2000-01-01', periods=100, freq='D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
ts
ts.resample('M').mean()
ts.resample('M', kind='period').mean()
# ### Downsampling
rng = pd.date_range('2000-01-01', periods=12, freq='T')
ts = pd.Series(np.arange(12), index=rng)
ts
ts.resample('5min', closed='left').sum()
ts.resample('5min', closed='right').sum()
ts.resample('5min', closed='right', label='right').sum()
ts.resample('5min', closed='right',
label='right', loffset='-1s').sum()
# #### Open-High-Low-Close (OHLC) resampling
#%%
ts.resample('5min').ohlc()
# ### Upsampling and Interpolation
#%%
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'])
frame
df_daily = frame.resample('D').asfreq()
df_daily
frame.resample('D').ffill()
frame.resample('D').ffill(limit=2)
frame.resample('W-THU').ffill()
# ### Resampling with Periods
frame = pd.DataFrame(np.random.randn(24, 4),
index=pd.period_range('1-2000', '12-2001', freq='M'),
columns=['Colorado', 'Texas', 'New York', 'Ohio'])
frame[:5]
annual_frame = frame.resample('A-DEC').mean()
annual_frame
# Q-DEC: Quarterly, year ending in December
annual_frame.resample('Q-DEC').ffill()
# ## Moving Window Functions
#%%
close_px_all = pd.read_csv('examples/stock_px_2.csv',
parse_dates=True, index_col=0)
close_px = close_px_all[['AAPL', 'MSFT', 'XOM']]
close_px = close_px.resample('B').ffill()
close_px.AAPL.plot()
close_px.AAPL.rolling(250).mean().plot()
appl_std250 = close_px.AAPL.rolling(250, min_periods=10).std()
appl_std250
appl_std250.plot()
close_px.rolling('20D').mean()
aapl_px = close_px.AAPL['2006':'2007']
ma60 = aapl_px.rolling(30, min_periods=20).mean()
ewma60 = aapl_px.ewm(alpha=0.1).mean()
aapl_px.plot()
ma60.plot(style='k--', label='Simple MA')
ewma60.plot(style='k-', label='EW MA')
plt.legend()
# ### Binary Moving Window Functions
#%%
spx_px = close_px_all['SPX']
spx_rets = spx_px.pct_change()
returns = close_px.pct_change()
corr = returns.AAPL.rolling(125, min_periods=100).corr(spx_rets)
corr
corr = returns.rolling(125, min_periods=100).corr(spx_rets)
corr.plot()
corr
# ### User-Defined Moving Window Functions
#%%
from scipy.stats import percentileofscore
score_at_2percent = lambda x: percentileofscore(x, 0.02)
result = returns.AAPL.rolling(250).apply(score_at_2percent)
result.plot()