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]:
0      1959.0
1      1959.0
2      1959.0
3      1959.0
4      1960.0
5      1960.0
6      1960.0
7      1960.0
8      1961.0
9      1961.0
10     1961.0
11     1961.0
12     1962.0
13     1962.0
14     1962.0
15     1962.0
16     1963.0
17     1963.0
18     1963.0
19     1963.0
20     1964.0
21     1964.0
22     1964.0
23     1964.0
24     1965.0
25     1965.0
26     1965.0
27     1965.0
28     1966.0
29     1966.0
        ...  
173    2002.0
174    2002.0
175    2002.0
176    2003.0
177    2003.0
178    2003.0
179    2003.0
180    2004.0
181    2004.0
182    2004.0
183    2004.0
184    2005.0
185    2005.0
186    2005.0
187    2005.0
188    2006.0
189    2006.0
190    2006.0
191    2006.0
192    2007.0
193    2007.0
194    2007.0
195    2007.0
196    2008.0
197    2008.0
198    2008.0
199    2008.0
200    2009.0
201    2009.0
202    2009.0
Name: year, dtype: float64

In [3]:
data.quarter


Out[3]:
0      1.0
1      2.0
2      3.0
3      4.0
4      1.0
5      2.0
6      3.0
7      4.0
8      1.0
9      2.0
10     3.0
11     4.0
12     1.0
13     2.0
14     3.0
15     4.0
16     1.0
17     2.0
18     3.0
19     4.0
20     1.0
21     2.0
22     3.0
23     4.0
24     1.0
25     2.0
26     3.0
27     4.0
28     1.0
29     2.0
      ... 
173    2.0
174    3.0
175    4.0
176    1.0
177    2.0
178    3.0
179    4.0
180    1.0
181    2.0
182    3.0
183    4.0
184    1.0
185    2.0
186    3.0
187    4.0
188    1.0
189    2.0
190    3.0
191    4.0
192    1.0
193    2.0
194    3.0
195    4.0
196    1.0
197    2.0
198    3.0
199    4.0
200    1.0
201    2.0
202    3.0
Name: quarter, dtype: float64

In [4]:
index = pd.PeriodIndex(year=data.year, quarter=data.quarter, freq='Q-DEC')
index


Out[4]:
PeriodIndex(['1959Q1', '1959Q2', '1959Q3', '1959Q4', '1960Q1', '1960Q2',
             '1960Q3', '1960Q4', '1961Q1', '1961Q2',
             ...
             '2007Q2', '2007Q3', '2007Q4', '2008Q1', '2008Q2', '2008Q3',
             '2008Q4', '2009Q1', '2009Q2', '2009Q3'],
            dtype='int64', length=203, freq='Q-DEC')

In [5]:
data.index = index
data.infl


Out[5]:
1959Q1    0.00
1959Q2    2.34
1959Q3    2.74
1959Q4    0.27
1960Q1    2.31
1960Q2    0.14
1960Q3    2.70
1960Q4    1.21
1961Q1   -0.40
1961Q2    1.47
1961Q3    0.80
1961Q4    0.80
1962Q1    2.26
1962Q2    0.13
1962Q3    2.11
1962Q4    0.79
1963Q1    0.53
1963Q2    2.75
1963Q3    0.78
1963Q4    2.46
1964Q1    0.13
1964Q2    0.90
1964Q3    1.29
1964Q4    2.05
1965Q1    1.28
1965Q2    2.54
1965Q3    0.89
1965Q4    2.90
1966Q1    4.99
1966Q2    2.10
          ... 
2002Q2    1.56
2002Q3    2.66
2002Q4    3.08
2003Q1    1.31
2003Q2    1.09
2003Q3    2.60
2003Q4    3.02
2004Q1    2.35
2004Q2    3.61
2004Q3    3.58
2004Q4    2.09
2005Q1    4.15
2005Q2    1.85
2005Q3    9.14
2005Q4    0.40
2006Q1    2.60
2006Q2    3.97
2006Q3   -1.58
2006Q4    3.30
2007Q1    4.58
2007Q2    2.75
2007Q3    3.45
2007Q4    6.38
2008Q1    2.82
2008Q2    8.53
2008Q3   -3.16
2008Q4   -8.79
2009Q1    0.94
2009Q2    3.37
2009Q3    3.56
Freq: Q-DEC, Name: infl, dtype: float64

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')


d:\Anaconda2\lib\site-packages\ipykernel\__main__.py:4: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).mean()
Out[6]:
2000-01-31   -0.144361
2000-02-29    0.140456
2000-03-31    0.320680
2000-04-30    0.150075
Freq: M, dtype: float64

In [7]:
ts.resample('M', kind='period').mean()


Out[7]:
2000-01   -0.144361
2000-02    0.140456
2000-03    0.320680
2000-04    0.150075
Freq: M, dtype: float64

In [8]:
rng = pd.date_range('1/1/2000', periods=12, freq='T')
ts = Series(np.arange(12), index=rng)
ts


Out[8]:
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

In [9]:
ts.resample('5min', closed='right').sum()


Out[9]:
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

In [10]:
ts.resample('5min', closed='left').sum()


Out[10]:
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

In [11]:
ts.resample('5min', loffset='-1s').sum()


Out[11]:
1999-12-31 23:59:59    10
2000-01-01 00:04:59    35
2000-01-01 00:09:59    21
Freq: 5T, dtype: int32

In [12]:
ts.resample('5min').ohlc()


Out[12]:
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

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]:
1    15
2    45
3    75
4    95
dtype: int32

In [14]:
ts.groupby(lambda x: x.weekday).mean()


Out[14]:
0    47.5
1    48.5
2    49.5
3    50.5
4    51.5
5    49.0
6    50.0
dtype: float64

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]:
Colorado Texas New York Ohio
2000-01-05 0.994764 -0.358266 0.364051 -0.564474
2000-01-12 1.407735 1.392605 0.761098 -0.196559

In [16]:
df_daily = frame.resample('D')
df_daily


d:\Anaconda2\lib\site-packages\IPython\lib\pretty.py:108: FutureWarning: .resample() is now a deferred operation
use .resample(...).mean() instead of .resample(...)
  return getattr(obj, attr, default)
Out[16]:
Colorado Texas New York Ohio
2000-01-05 0.994764 -0.358266 0.364051 -0.564474
2000-01-06 NaN NaN NaN NaN
2000-01-07 NaN NaN NaN NaN
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 1.407735 1.392605 0.761098 -0.196559

In [17]:
frame.resample('D', fill_method='ffill')


d:\Anaconda2\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: fill_method is deprecated to .resample()
the new syntax is .resample(...).ffill()
  if __name__ == '__main__':
Out[17]:
Colorado Texas New York Ohio
2000-01-05 0.994764 -0.358266 0.364051 -0.564474
2000-01-06 0.994764 -0.358266 0.364051 -0.564474
2000-01-07 0.994764 -0.358266 0.364051 -0.564474
2000-01-08 0.994764 -0.358266 0.364051 -0.564474
2000-01-09 0.994764 -0.358266 0.364051 -0.564474
2000-01-10 0.994764 -0.358266 0.364051 -0.564474
2000-01-11 0.994764 -0.358266 0.364051 -0.564474
2000-01-12 1.407735 1.392605 0.761098 -0.196559

In [18]:
frame.resample('D').ffill(limit=2)


Out[18]:
Colorado Texas New York Ohio
2000-01-05 0.994764 -0.358266 0.364051 -0.564474
2000-01-06 0.994764 -0.358266 0.364051 -0.564474
2000-01-07 0.994764 -0.358266 0.364051 -0.564474
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 1.407735 1.392605 0.761098 -0.196559

In [19]:
frame.resample('W-THU').ffill()


Out[19]:
Colorado Texas New York Ohio
2000-01-06 0.994764 -0.358266 0.364051 -0.564474
2000-01-13 1.407735 1.392605 0.761098 -0.196559

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]:
Colorado Texas New York Ohio
2000-01 0.068682 0.157876 0.220901 0.374006
2000-02 -1.367299 0.387719 0.685010 -1.190159
2000-03 -0.366935 1.296431 -0.003281 -0.724946
2000-04 0.175977 -0.503338 1.601330 0.319548
2000-05 -0.322672 -0.862579 0.299727 -0.234991

In [21]:
annual_frame = frame.resample('A-DEC').mean()
annual_frame


Out[21]:
Colorado Texas New York Ohio
2000 -0.057320 -0.115391 -0.073525 -0.453801
2001 0.126841 -0.121661 0.208073 0.176911

In [22]:
# Q-DEC: 季度型(每年以12月结束)
annual_frame.resample('Q-DEC').ffill()


Out[22]:
Colorado Texas New York Ohio
2000Q1 -0.057320 -0.115391 -0.073525 -0.453801
2000Q2 -0.057320 -0.115391 -0.073525 -0.453801
2000Q3 -0.057320 -0.115391 -0.073525 -0.453801
2000Q4 -0.057320 -0.115391 -0.073525 -0.453801
2001Q1 0.126841 -0.121661 0.208073 0.176911
2001Q2 0.126841 -0.121661 0.208073 0.176911
2001Q3 0.126841 -0.121661 0.208073 0.176911
2001Q4 0.126841 -0.121661 0.208073 0.176911

In [23]:
# Q-DEC: 季度型(每年以12月结束)
annual_frame.resample('Q-DEC', convention='start').ffill()


Out[23]:
Colorado Texas New York Ohio
2000Q1 -0.057320 -0.115391 -0.073525 -0.453801
2000Q2 -0.057320 -0.115391 -0.073525 -0.453801
2000Q3 -0.057320 -0.115391 -0.073525 -0.453801
2000Q4 -0.057320 -0.115391 -0.073525 -0.453801
2001Q1 0.126841 -0.121661 0.208073 0.176911
2001Q2 0.126841 -0.121661 0.208073 0.176911
2001Q3 0.126841 -0.121661 0.208073 0.176911
2001Q4 0.126841 -0.121661 0.208073 0.176911

In [24]:
annual_frame.resample('Q-MAR').ffill()


Out[24]:
Colorado Texas New York Ohio
2000Q4 -0.057320 -0.115391 -0.073525 -0.453801
2001Q1 -0.057320 -0.115391 -0.073525 -0.453801
2001Q2 -0.057320 -0.115391 -0.073525 -0.453801
2001Q3 -0.057320 -0.115391 -0.073525 -0.453801
2001Q4 0.126841 -0.121661 0.208073 0.176911
2002Q1 0.126841 -0.121661 0.208073 0.176911
2002Q2 0.126841 -0.121661 0.208073 0.176911
2002Q3 0.126841 -0.121661 0.208073 0.176911

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]:
AAPL MSFT XOM
2003-01-02 7.40 21.11 29.22
2003-01-03 7.45 21.14 29.24
2003-01-06 7.45 21.52 29.96
2003-01-07 7.43 21.93 28.95
2003-01-08 7.28 21.31 28.83
2003-01-09 7.34 21.93 29.44
2003-01-10 7.36 21.97 29.03
2003-01-13 7.32 22.16 28.91
2003-01-14 7.30 22.39 29.17
2003-01-15 7.22 22.11 28.77
2003-01-16 7.31 21.75 28.90
2003-01-17 7.05 20.22 28.60
2003-01-21 7.01 20.17 27.94
2003-01-22 6.94 20.04 27.58
2003-01-23 7.09 20.54 27.52
2003-01-24 6.90 19.59 26.93
2003-01-27 7.07 19.32 26.21
2003-01-28 7.29 19.18 26.90
2003-01-29 7.47 19.61 27.88
2003-01-30 7.16 18.95 27.37
2003-01-31 7.18 18.65 28.13
2003-02-03 7.33 19.08 28.52
2003-02-04 7.30 18.59 28.52
2003-02-05 7.22 18.45 28.11
2003-02-06 7.22 18.63 27.87
2003-02-07 7.07 18.30 27.66
2003-02-10 7.18 18.62 27.87
2003-02-11 7.18 18.25 27.67
2003-02-12 7.20 18.25 27.12
2003-02-13 7.27 18.46 27.47
... ... ... ...
2011-09-02 374.05 25.80 72.14
2011-09-06 379.74 25.51 71.15
2011-09-07 383.93 26.00 73.65
2011-09-08 384.14 26.22 72.82
2011-09-09 377.48 25.74 71.01
2011-09-12 379.94 25.89 71.84
2011-09-13 384.62 26.04 71.65
2011-09-14 389.30 26.50 72.64
2011-09-15 392.96 26.99 74.01
2011-09-16 400.50 27.12 74.55
2011-09-19 411.63 27.21 73.70
2011-09-20 413.45 26.98 74.01
2011-09-21 412.14 25.99 71.97
2011-09-22 401.82 25.06 69.24
2011-09-23 404.30 25.06 69.31
2011-09-26 403.17 25.44 71.72
2011-09-27 399.26 25.67 72.91
2011-09-28 397.01 25.58 72.07
2011-09-29 390.57 25.45 73.88
2011-09-30 381.32 24.89 72.63
2011-10-03 374.60 24.53 71.15
2011-10-04 372.50 25.34 72.83
2011-10-05 378.25 25.89 73.95
2011-10-06 377.37 26.34 73.89
2011-10-07 369.80 26.25 73.56
2011-10-10 388.81 26.94 76.28
2011-10-11 400.29 27.00 76.27
2011-10-12 402.19 26.96 77.16
2011-10-13 408.43 27.18 76.37
2011-10-14 422.00 27.27 78.11

2214 rows × 3 columns


In [26]:
close_px['AAPL'].plot()


Out[26]:
<matplotlib.axes._subplots.AxesSubplot at 0x9b35eb8>

In [27]:
close_px.ix['2009'].plot()


Out[27]:
<matplotlib.axes._subplots.AxesSubplot at 0x9d89470>

In [28]:
close_px['AAPL'].ix['01-2011':'03-2011'].plot()


Out[28]:
<matplotlib.axes._subplots.AxesSubplot at 0xa0c7e10>

In [29]:
appl_q = close_px['AAPL'].resample('Q-DEC').ffill()
appl_q.ix['2009':].plot()


Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0xa043208>

In [30]:
close_px.AAPL.plot()
pd.rolling_mean(close_px.AAPL, 250).plot()


d:\Anaconda2\lib\site-packages\ipykernel\__main__.py:2: FutureWarning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with 
	Series.rolling(window=250,center=False).mean()
  from ipykernel import kernelapp as app
Out[30]:
<matplotlib.axes._subplots.AxesSubplot at 0xa0c72e8>

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]:
<matplotlib.axes._subplots.AxesSubplot at 0xa2faa20>

In [32]:
appl_std250 = pd.rolling_std(close_px.AAPL, 250, min_periods=10)
appl_std250[5:12]


d:\Anaconda2\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: pd.rolling_std is deprecated for Series and will be removed in a future version, replace with 
	Series.rolling(min_periods=10,window=250,center=False).std()
  if __name__ == '__main__':
Out[32]:
2003-01-09         NaN
2003-01-10         NaN
2003-01-13         NaN
2003-01-14         NaN
2003-01-15    0.077496
2003-01-16    0.074760
2003-01-17    0.112368
Name: AAPL, dtype: float64

In [33]:
appl_std250 = close_px.AAPL.rolling(min_periods=10, window=250,
                                   center=False).std()
appl_std250[5:12]


Out[33]:
2003-01-09         NaN
2003-01-10         NaN
2003-01-13         NaN
2003-01-14         NaN
2003-01-15    0.077496
2003-01-16    0.074760
2003-01-17    0.112368
Name: AAPL, dtype: float64

In [34]:
close_px.AAPL.rolling(window=250, center=False).mean().plot()
appl_std250.plot()


Out[34]:
<matplotlib.axes._subplots.AxesSubplot at 0xa71d198>

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]:
<matplotlib.axes._subplots.AxesSubplot at 0xa917be0>

In [36]:
close_px.apply(expanding_mean).plot()


Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0xa2fa588>

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]:
<matplotlib.axes._subplots.AxesSubplot at 0xc3bb0b8>

In [41]:
corr = returns.rolling(125, min_periods=100).corr(spx_rets)
corr.plot()


Out[41]:
<matplotlib.axes._subplots.AxesSubplot at 0xc3ef898>

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]:
<matplotlib.axes._subplots.AxesSubplot at 0xe6f5908>

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]:
2000-01-01 00:00:00.000   -0.234432
2000-01-01 00:00:00.010    0.378522
2000-01-01 00:00:00.020    0.202585
2000-01-01 00:00:00.030   -1.467881
2000-01-01 00:00:00.040   -0.710932
2000-01-01 00:00:00.050   -0.934262
2000-01-01 00:00:00.060    0.961889
2000-01-01 00:00:00.070    1.270962
2000-01-01 00:00:00.080   -0.226152
2000-01-01 00:00:00.090    0.792114
2000-01-01 00:00:00.100    0.111563
2000-01-01 00:00:00.110   -0.057751
2000-01-01 00:00:00.120    0.600989
2000-01-01 00:00:00.130   -0.521402
2000-01-01 00:00:00.140    0.881906
2000-01-01 00:00:00.150   -0.035131
2000-01-01 00:00:00.160   -1.892933
2000-01-01 00:00:00.170    0.969916
2000-01-01 00:00:00.180    0.802980
2000-01-01 00:00:00.190   -0.398881
2000-01-01 00:00:00.200    0.264653
2000-01-01 00:00:00.210    0.976684
2000-01-01 00:00:00.220   -0.633083
2000-01-01 00:00:00.230   -0.796238
2000-01-01 00:00:00.240   -1.004481
2000-01-01 00:00:00.250    0.377174
2000-01-01 00:00:00.260   -1.205474
2000-01-01 00:00:00.270    1.554846
2000-01-01 00:00:00.280   -0.656790
2000-01-01 00:00:00.290    0.302315
                             ...   
2000-01-02 03:46:39.700    1.422177
2000-01-02 03:46:39.710    0.344265
2000-01-02 03:46:39.720    0.685043
2000-01-02 03:46:39.730   -0.302538
2000-01-02 03:46:39.740    0.243349
2000-01-02 03:46:39.750   -1.673716
2000-01-02 03:46:39.760   -0.159731
2000-01-02 03:46:39.770    1.208592
2000-01-02 03:46:39.780    0.227376
2000-01-02 03:46:39.790    0.861006
2000-01-02 03:46:39.800   -1.310320
2000-01-02 03:46:39.810    0.982609
2000-01-02 03:46:39.820    2.482026
2000-01-02 03:46:39.830    1.728614
2000-01-02 03:46:39.840   -0.215756
2000-01-02 03:46:39.850    1.830746
2000-01-02 03:46:39.860   -0.036129
2000-01-02 03:46:39.870    1.323612
2000-01-02 03:46:39.880    0.332848
2000-01-02 03:46:39.890   -0.795828
2000-01-02 03:46:39.900   -0.213555
2000-01-02 03:46:39.910   -0.746446
2000-01-02 03:46:39.920   -0.067730
2000-01-02 03:46:39.930   -0.070518
2000-01-02 03:46:39.940    1.528452
2000-01-02 03:46:39.950   -0.672578
2000-01-02 03:46:39.960   -0.341267
2000-01-02 03:46:39.970   -1.018409
2000-01-02 03:46:39.980   -1.334268
2000-01-02 03:46:39.990    0.266078
Freq: 10L, dtype: float64

In [59]:
ts.resample('15min').ohlc()


Out[59]:
open high low close
2000-01-01 00:00:00 -0.234432 5.190503 -4.020423 1.445252
2000-01-01 00:15:00 -0.257785 4.404629 -4.128218 -1.520103
2000-01-01 00:30:00 0.375895 4.229061 -4.200664 -0.412467
2000-01-01 00:45:00 0.566286 4.372822 -4.110084 -0.373375
2000-01-01 01:00:00 0.715409 4.104241 -4.631752 1.308574
2000-01-01 01:15:00 -2.322977 4.104785 -4.013712 -3.926656
2000-01-01 01:30:00 -1.595943 4.093280 -4.538662 0.273281
2000-01-01 01:45:00 0.789271 4.108138 -4.099372 0.196814
2000-01-01 02:00:00 -0.300155 4.156349 -4.249065 -1.305966
2000-01-01 02:15:00 0.500542 4.212239 -3.846975 0.432426
2000-01-01 02:30:00 0.944645 4.130672 -4.464138 0.893384
2000-01-01 02:45:00 -0.473789 4.293513 -4.048044 0.974603
2000-01-01 03:00:00 -0.341896 4.412184 -4.535259 0.800979
2000-01-01 03:15:00 -1.584846 4.902963 -4.945157 -0.615713
2000-01-01 03:30:00 0.076783 4.327881 -4.353654 -0.702475
2000-01-01 03:45:00 -0.742583 4.334743 -4.413586 0.351867
2000-01-01 04:00:00 1.530760 4.273816 -4.405144 1.506829
2000-01-01 04:15:00 0.769679 4.318688 -4.234863 0.235854
2000-01-01 04:30:00 0.203177 4.539701 -4.696258 -0.042558
2000-01-01 04:45:00 0.658558 4.276452 -4.139434 0.550101
2000-01-01 05:00:00 1.931949 4.224446 -4.551178 0.431762
2000-01-01 05:15:00 -0.905624 4.076983 -4.731003 -1.012359
2000-01-01 05:30:00 -1.987710 4.601702 -4.420986 0.340486
2000-01-01 05:45:00 -0.813762 3.878908 -4.229333 0.270599
2000-01-01 06:00:00 0.492809 4.733967 -4.108948 -0.521158
2000-01-01 06:15:00 -0.004360 4.617377 -4.340426 0.330032
2000-01-01 06:30:00 1.745159 4.065813 -3.989345 -0.512343
2000-01-01 06:45:00 -0.241868 4.082123 -3.882575 0.103562
2000-01-01 07:00:00 -1.782115 4.019442 -4.304563 0.077412
2000-01-01 07:15:00 0.480155 4.122526 -4.202533 -1.456342
... ... ... ... ...
2000-01-01 20:30:00 0.694074 4.418162 -4.178089 -0.823772
2000-01-01 20:45:00 -0.150585 4.639111 -3.887742 0.845009
2000-01-01 21:00:00 1.249144 4.065857 -4.464829 -1.301421
2000-01-01 21:15:00 -0.216787 4.143089 -4.445108 -1.339508
2000-01-01 21:30:00 0.177842 4.374681 -4.232574 0.128411
2000-01-01 21:45:00 0.335368 4.421126 -4.368641 0.416973
2000-01-01 22:00:00 1.263491 4.489069 -4.628509 0.415250
2000-01-01 22:15:00 0.125289 4.451672 -4.255516 1.626013
2000-01-01 22:30:00 0.037165 4.250430 -4.252261 -0.442763
2000-01-01 22:45:00 0.835200 4.374860 -4.155072 -0.104832
2000-01-01 23:00:00 -0.814341 4.650478 -4.381173 -0.679866
2000-01-01 23:15:00 -0.258551 3.950191 -4.233832 -1.322188
2000-01-01 23:30:00 2.107063 4.295818 -4.024385 -1.140531
2000-01-01 23:45:00 -1.179765 4.140243 -4.497083 0.182881
2000-01-02 00:00:00 0.049110 4.280584 -4.152721 -1.091786
2000-01-02 00:15:00 -1.885503 4.221835 -4.289804 -0.617427
2000-01-02 00:30:00 0.089022 4.682692 -4.926828 0.229220
2000-01-02 00:45:00 0.139379 4.883904 -4.312094 -0.260262
2000-01-02 01:00:00 -1.270427 4.182864 -4.417655 0.585099
2000-01-02 01:15:00 -0.005401 4.125872 -4.270931 0.078164
2000-01-02 01:30:00 1.207147 4.911407 -3.963174 0.037994
2000-01-02 01:45:00 -0.362877 4.471261 -4.336267 -1.277578
2000-01-02 02:00:00 -0.713644 4.383540 -4.224385 1.168411
2000-01-02 02:15:00 0.752763 5.323152 -4.597021 -0.185741
2000-01-02 02:30:00 -1.259552 4.467640 -4.929934 1.162757
2000-01-02 02:45:00 0.253154 4.330509 -4.108305 0.186861
2000-01-02 03:00:00 0.280097 4.222372 -4.473753 -1.037308
2000-01-02 03:15:00 1.456687 4.127920 -4.449995 2.124799
2000-01-02 03:30:00 -0.176945 4.647183 -4.526836 1.139303
2000-01-02 03:45:00 -1.187229 4.365611 -4.025375 0.266078

112 rows × 4 columns


In [61]:
%timeit ts.resample('15min').ohlc()


10 loops, best of 3: 178 ms per loop

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()


1 loop, best of 3: 248 ms per loop