In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pandas import DataFrame, Series
import datetime as datetime
In [2]:
data = pd.read_csv('ch08/macrodata.csv')
data.year
Out[2]:
In [3]:
data.quarter
Out[3]:
In [4]:
index = pd.PeriodIndex(year=data.year, quarter=data.quarter, freq='Q-DEC')
index
Out[4]:
In [5]:
data.index = index
data.infl
Out[5]:
In [6]:
from numpy.random import randn
rng = pd.date_range('1/1/2000', periods=100, freq='D')
ts = Series(randn(len(rng)), index=rng)
ts.resample('M', how='mean')
Out[6]:
In [7]:
ts.resample('M', kind='period').mean()
Out[7]:
In [8]:
rng = pd.date_range('1/1/2000', periods=12, freq='T')
ts = Series(np.arange(12), index=rng)
ts
Out[8]:
In [9]:
ts.resample('5min', closed='right').sum()
Out[9]:
In [10]:
ts.resample('5min', closed='left').sum()
Out[10]:
In [11]:
ts.resample('5min', loffset='-1s').sum()
Out[11]:
In [12]:
ts.resample('5min').ohlc()
Out[12]:
In [13]:
rng = pd.date_range('1/1/2000', periods=100, freq='D')
ts = Series(np.arange(100), index=rng)
ts.groupby(lambda x: x.month).mean()
Out[13]:
In [14]:
ts.groupby(lambda x: x.weekday).mean()
Out[14]:
In [15]:
frame = 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[:5]
Out[15]:
In [16]:
df_daily = frame.resample('D')
df_daily
Out[16]:
In [17]:
frame.resample('D', fill_method='ffill')
Out[17]:
In [18]:
frame.resample('D').ffill(limit=2)
Out[18]:
In [19]:
frame.resample('W-THU').ffill()
Out[19]:
In [20]:
frame = DataFrame(np.random.randn(24, 4),
index=pd.period_range('1-2000', '12-2001', freq='M'),
columns=['Colorado', 'Texas', 'New York', 'Ohio'])
frame[:5]
Out[20]:
In [21]:
annual_frame = frame.resample('A-DEC').mean()
annual_frame
Out[21]:
In [22]:
# Q-DEC: 季度型(每年以12月结束)
annual_frame.resample('Q-DEC').ffill()
Out[22]:
In [23]:
# Q-DEC: 季度型(每年以12月结束)
annual_frame.resample('Q-DEC', convention='start').ffill()
Out[23]:
In [24]:
annual_frame.resample('Q-MAR').ffill()
Out[24]:
In [25]:
close_px_all = pd.read_csv('ch09/stock_px.csv', parse_dates=True, index_col=0)
close_px = close_px_all[['AAPL', 'MSFT', 'XOM']]
cloe_px = close_px.resample('B').ffill()
close_px
Out[25]:
In [26]:
close_px['AAPL'].plot()
Out[26]:
In [27]:
close_px.ix['2009'].plot()
Out[27]:
In [28]:
close_px['AAPL'].ix['01-2011':'03-2011'].plot()
Out[28]:
In [29]:
appl_q = close_px['AAPL'].resample('Q-DEC').ffill()
appl_q.ix['2009':].plot()
Out[29]:
In [30]:
close_px.AAPL.plot()
pd.rolling_mean(close_px.AAPL, 250).plot()
Out[30]:
In [31]:
close_px.AAPL.plot()
close_px.AAPL.rolling(window=250, center=False).mean().plot()
#pd.rolling_mean(close_px.AAPL, 250).plot()
Out[31]:
In [32]:
appl_std250 = pd.rolling_std(close_px.AAPL, 250, min_periods=10)
appl_std250[5:12]
Out[32]:
In [33]:
appl_std250 = close_px.AAPL.rolling(min_periods=10, window=250,
center=False).std()
appl_std250[5:12]
Out[33]:
In [34]:
close_px.AAPL.rolling(window=250, center=False).mean().plot()
appl_std250.plot()
Out[34]:
In [35]:
# 通过rolling_mean定义扩展平均
expanding_mean = lambda x: x.rolling(len(x), min_periods=1).mean()
close_px.rolling(60).mean().plot(logy=True)
Out[35]:
In [36]:
close_px.apply(expanding_mean).plot()
Out[36]:
In [37]:
#close_px.rolling(60).corr()
In [39]:
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True, figsize=(12, 7))
aapl_px = close_px.AAPL['2005':'2009']
ma60 = aapl_px.rolling(60, min_periods=50).mean()
ewma60 = aapl_px.ewm(span=60).mean()
aapl_px.plot(style='k-', ax=axes[0])
ma60.plot(style='k--', ax=axes[0])
aapl_px.plot(style='k-', ax=axes[1])
ewma60.plot(style='k--', ax=axes[1])
axes[0].set_title('Simple MA')
axes[1].set_title('Exponentially-weighted MA')
plt.plot()
Out[39]:
In [40]:
spx_px =close_px_all['SPX']
spx_rets = spx_px / spx_px.shift(1) - 1
returns = close_px.pct_change()
corr = returns.AAPL.rolling(125, min_periods=100).corr(spx_rets)
corr.plot()
Out[40]:
In [41]:
corr = returns.rolling(125, min_periods=100).corr(spx_rets)
corr.plot()
Out[41]:
In [42]:
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()
Out[42]:
In [58]:
rng = pd.date_range('1/1/2000', periods=10000000, freq='10ms')
ts = Series(np.random.randn(len(rng)), index=rng)
ts
Out[58]:
In [59]:
ts.resample('15min').ohlc()
Out[59]:
In [61]:
%timeit ts.resample('15min').ohlc()
In [62]:
rng = pd.date_range('1/1/2000', periods=10000000, freq='1s')
ts = Series(np.random.randn(len(rng)), index=rng)
%timeit ts.resample('15s').ohlc()