<jupyter_start><jupyter_text>four_hours = Hour(4)print(four_hours)<jupyter_code>pd.date_range('1/1/2000', '1/3/2000 23:59', freq='4h')
Hour(2) + Minute(30)
pd.date_range('1/1/2000', periods=10, freq='1h30min')
HTML(Path('data/time_ser_freq.htm').read_text())
%%html
<h3>Week of Month Dates</h3>
rng = pd.date_range('1/1/2012', '9/1/2012', freq='WOM-3FRI')
list(rng)
%%html
<h2>Shifting Data</h2>
ts = Series(np.random.randn(4), index=pd.date_range('1/1/2000', periods=4, freq='M'))
ts
ts.shift(2)
ts.shift(-2)
ts / ts.shift(1) - 1
ts.shift(2, freq='M')
ts.shift(3, freq='D')
ts.shift(1, freq='3D')
ts.shift(1, freq='90T')
%%html
<h3>Shifting Dates with Offsets</h3>
now = datetime(2011, 11, 17)
now + 3 * Day()
now + MonthEnd()
now + MonthEnd(2)
offset = MonthEnd()
offset.rollforward(now)
offset.rollback(now)
ts = Series(np.random.randn(20), index=pd.date_range('1/15/2000', periods=20, freq='4d'))
ts.groupby(offset.rollforward).mean()
ts.resample('M').mean()
%%html
<h2>Time Zones</h2>
pytz.common_timezones[-5:]
tz = pytz.timezone('US/Eastern')
tz
%%html
<h3>Localization and Conversion</h3>
rng = pd.date_range('3/9/2012 9:30', periods=6, freq='D')
ts = Series(np.random.randn(len(rng)), index=rng)
print(ts.index.tz)
pd.date_range('3/9/2012', periods=10, freq='D', tz='UTC')
ts_utc = ts.tz_localize('UTC')
ts_utc
ts_utc.index
ts_utc.tz_convert('US/Eastern')
ts_eastern = ts.tz_localize('US/Eastern')
ts_eastern.tz_convert('UTC')
ts_eastern.tz_convert('Europe/Berlin')
ts.index.tz_localize('Asia/Shanghai')
%%html
<h3>Operations with Time Zone-Aware Timestamp Objects</h3>
stamp = pd.Timestamp('2011-03-12 04:00')
stamp_utc = stamp.tz_localize('utc')
stamp_utc.tz_convert('US/Eastern')
stamp_moscow = pd.Timestamp('2011-03-12 04:00', tz='Europe/Moscow')
stamp_moscow
stamp_utc.value
stamp_utc.tz_convert('US/Eastern').value
# 30 minutes before DST transition
stamp = pd.Timestamp('2012-03-12 01:30', tz='US/Eastern')
stamp
stamp + Hour()
# 90 minutes before DST transition
stamp = pd.Timestamp('2012-11-04 00:30', tz='US/Eastern')
stamp
stamp + 2 * Hour()
%%html
<h3>Operations Between Different Time Zones</h3>
rng = pd.date_range('3/7/2012 9:30', periods=10, freq='B')
ts = Series(np.random.randn(len(rng)), index=rng)
ts
ts1 = ts[:7].tz_localize('Europe/London')
ts2 = ts1[2:].tz_convert('Europe/Moscow')
result = ts1 + ts2
result.index
%%html
<h2>Periods and Period Arithmetic</h2>
p = pd.Period(2007, freq='A-DEC')
p
p + 5
p - 2
pd.Period('2014', freq='A-DEC') - p
rng = pd.period_range('1/1/2000', '6/30/2000', freq='M')
rng
Series(np.random.randn(6), index=rng)
values = ['2001Q3', '2002Q2', '2003Q1']
index = pd.PeriodIndex(values, freq='Q-DEC')
index
%%html
<h3>Period Frequency Conversion</h3>
p = pd.Period('2007', freq='A-DEC')
p.asfreq('M', how='start')
p.asfreq('M', how='end')
p = pd.Period('2007', freq='A-JUN')
p.asfreq('M', 'start')
p.asfreq('M', 'end')
p = pd.Period('2007-08', 'M')
p.asfreq('A-JUN')
rng = pd.period_range('2006', '2009', freq='A-DEC')
ts = Series(np.random.randn(len(rng)), index=rng)
ts
ts.asfreq('M', how='start')
ts.asfreq('B', how='end')
%%html
<h3>Quarterly Period Frequencies</h3>
p = pd.Period('2012Q4', freq='Q-JAN')
p
p.asfreq('D', 'start')
p.asfreq('D', 'end')
p4pm = (p.asfreq('B', 'e') - 1).asfreq('T', 's') + 16 * 60
p4pm
p4pm.to_timestamp()
rng = pd.period_range('2011Q3', '2012Q4', freq='Q-JAN')
ts = Series(np.arange(len(rng)), index=rng)
ts
new_rng = (rng.asfreq('B', 'e') - 1).asfreq('T', 's') + 16 * 60
ts.index = new_rng.to_timestamp()
ts
%%html
<h3>Converting Timestamps to Periods (and Back)</h3>
rng = pd.date_range('1/1/2000', periods=3, freq='M')
ts = Series(np.random.randn(3), index=rng)
pts = ts.to_period()
ts
rng = pd.date_range('1/29/2000', periods=6, freq='D')
ts2 = Series(np.random.randn(6), index=rng)
ts2.to_period('M')
pts = ts.to_period()
pts
pts.to_timestamp(how='end')
%%html
<h3>Creating a <code>PeriodIndex</code> from Arrays</h3>
data = pd.read_csv('data/macrodata.csv')
data.year
data.quarter
index = pd.PeriodIndex(year=data.year, quarter=data.quarter, freq='Q-DEC')
index
data.index = index
data.infl
%%html
<h2>Resampling and Frequency Conversion</h2>
<p><strong>Resampling</strong> refers to the process of converting a time series from one
frequency to another. Aggregating higher frequency data to lower frequency is called
<strong>downsampling</strong>, while converting lower frequency to higher frequency is
called <strong>upsampling</strong>.</p>
rng = pd.date_range('1/1/2000', periods=100, freq='D')
ts = Series(np.random.randn(len(rng)), index=rng)
ts.resample('M').mean()
ts.resample('M', kind='period').mean()
HTML(Path('data/resample_method_args.htm').read_text())
%%html
<h3>Downsampling</h3>
rng = pd.date_range('1/1/2000', periods=12, freq='T')
ts = Series(np.arange(12), index=rng)
ts
ts.resample('5min').sum()
ts.resample('5min', closed='left').sum()
ts.resample('5min', closed='left', label='left').sum()
ts.resample('5min', loffset='-1s').sum()
%%html
<h4>Open-High-Low-Close (OHLC) Resampling</h4>
ts.resample('5min').ohlc()
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()
ts.groupby(lambda x: x.weekday).mean()
%%html
<h3>Unsampling and Interpolation</h3>
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]
df_daily = frame.resample('D')
df_daily
frame.resample('D').ffill()
frame.resample('D', limit=2).ffill()
frame.resample('W-THU').ffill()
%%html
<h3>Resampling with Periods</h3>
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]
annual_frame = frame.resample('A-DEC').mean()
annual_frame
# Q-DEC: Quarterly, year ending in December
annual_frame.resample('Q-DEC').ffill()
annual_frame.resample('Q-DEC', convention='start').ffill()
annual_frame.resample('Q-MAR').ffill()
%%html
<h3>Time Series Plotting</h3>
close_px_all = pd.read_csv('data/stock_px.csv', parse_dates=True, index_col=0)
close_px = close_px_all[['AAPL', 'MSFT', 'XOM']]
close_px = close_px.resample('B').ffill()
close_px
close_px.ix['2009'].plot()
plt.show()
close_px['AAPL'].ix['01-2011':'03-2011'].plot()
aapl_q = close_px['AAPL'].resample('Q-DEC').ffill()
aapl_q.ix['2009':].plot()
%%html
<h3>Moving Window Functions</h3>
close_px.AAPL.plot()
pd.rolling_mean(close_px.AAPL, 250).plot()
appl_std250 = pd.rolling_std(close_px.AAPL, 250, min_periods=10)
appl_std250
appl_std250.plot()
# Define expanding mean in terms of rolling_mean
expanding_mean = lambda x: rolling_mean(x, len(x), min_periods=1)
pd.rolling_mean(close_px, 60).plot(logy=True)
HTML(Path('data/moving_funcs.html').read_text())
%%html
<h3>Exponentially-Weighted Functions</h3>
%%html
<p>An alternative to using a static window size with equally-weighted observations is to
speciry a constant <strong>decay factor</strong> to give more weight to more recent
observations.</p>
<p>If $\mathrm{ma}_t$ is the moving average result at time $t$ and $x$ is the time series
in question, each value in the result is computed as $\mathrm{ma}_{t-1}+x_{-t}(a-1)$,
where $a$ is the decay factor, which can be specified using a <strong>span</strong>, which
makes the result comparable to a simple moving window function with window size equal to the
span.</p>
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True, figsize=(12, 7))
aapl_px = close_px.AAPL['2005':'2009']
ma60 = pd.rolling_mean(aapl_px, 60, min_periods=50)
ewma60 = pd.ewma(aapl_px, span=60)
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')
%%html
<h3>Binary Moving Window Functions</h3>
spx_rets = spx_px / spx_px.shift(1) - 1
returns = close_px.pct_change()
corr = pd.rolling_corr(returns.AAPL, spx_rets, 125, min_periods=100)
corr.plot()
%%html
<h3>User-Defined Moving Window Functions</h3>
score_at_2percent = lambda x: percentileofscore(x, 0.02)
returns = close_px.pct_change()
result = pd.rolling_apply(returns.AAPL, 250, score_at_2percent)
result.plot()<jupyter_output>/home/fuzzy/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:3: FutureWarning: pd.rolling_apply is deprecated for Series and will be removed in a future version, replace with 
	Series.rolling(window=250,center=False).apply(func=<function>,args=<tuple>,kwargs=<dict>)
  This is separate from the ipykernel package so we can avoid doing imports until
{'fraction_non_alphanumeric': 0.1956521739130435, 'fraction_numerical': 0.05636070853462158, 'mean_word_length': 6.06336311941511, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '595260', 'license': 'Apache-2.0', 'n_tokens_mistral': 3835, 'n_tokens_neox': 3514, 'n_words': 694, 'path': 'nb/time_series.ipynb', 'repo_name': 'fuzzyklein/hw-4.2.0', 'size': '8694'}