# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Time-series

# * https://jakevdp.github.io/PythonDataScienceHandbook/03.11-working-with-time-series.html

# %matplotlib inline
# %load_ext autoreload
# %autoreload 2
import os
import sys
p = os.path.join(os.path.dirname('__file__'), '..')
sys.path.append(p)
import seaborn; seaborn.set()
from common import *

# ### Data

data = pd.read_csv('../data/FremontBridge.csv', index_col='Date', parse_dates=True)
data.head()

data.columns = ['West', 'East']
data['Total'] = data.eval('West + East')

data.dropna().describe()

data.plot()
plt.ylabel('Hourly Bicycle Count');

# ### Resampling

"""goog.plot(alpha=0.5, style='-')
goog.resample('BA').mean().plot(style=':')
goog.asfreq('BA').plot(style='--');
plt.legend(['input', 'resample', 'asfreq'],
           loc='upper left');
""";

# The hourly data points are too dense
# Lets resample to the SUM over WEEK
weekly = data.resample('W').sum()
weekly.plot(style=[':', '--', '-'])
plt.ylabel('Weekly bicycle count');

# ### Rolling Windows

# +
# 30 day rolling mean of our data
# making sure to center the window?

daily = data.resample('D').sum()
daily.rolling(30, center=True).sum().plot(style=[':', '--', '-'])
plt.ylabel('mean hourly count');

# +
# Gaussian-weighted mean
# Weight nearby observations more than distant observations

"""
The jaggedness of the result is due to the hard cutoff of the window. We can get a smoother version of a rolling mean using a window function–for example, a Gaussian window. The following code specifies both the width of the window (we chose 50 days) and the width of the Gaussian within the window (we chose 10 days):
""";
# -

daily.rolling(50, center=True,
              win_type='gaussian').sum(std=10).plot(style=[':', '--', '-']);

# ### Aggregations

# Get the mean values over a typical day
# Output is strongly bimodal with peaks at beginning/end of day
by_time = data.groupby(data.index.time).mean()
hourly_ticks = 4 * 60 * 60 * np.arange(6)
by_time.plot(xticks=hourly_ticks, style=[':', '--', '-']);

# How things change by day of week
# Pandas datetime columns have lots of interesting ways to split
by_weekday = data.groupby(data.index.dayofweek).mean()
by_weekday.index = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
by_weekday.plot(style=[':', '--', '-']);

# +
# Weekend vs Weekday

weekend = np.where(data.index.weekday < 5, 'Weekday', 'Weekend')
by_time = data.groupby([weekend, data.index.time]).mean()

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2, figsize=(14, 5))
by_time.ix['Weekday'].plot(ax=ax[0], title='Weekdays',
                           xticks=hourly_ticks, style=[':', '--', '-'])
by_time.ix['Weekend'].plot(ax=ax[1], title='Weekends',
                           xticks=hourly_ticks, style=[':', '--', '-']);
# -

# ### Date types

from datetime import datetime
datetime(year=2015, month=7, day=4)

from dateutil import parser
date = parser.parse("4th of July, 2015")
date

# Create a sequence of dates
date = np.array('2015-07-05', dtype=np.datetime64)
mydates = np.arange(12) + date
mydates

# Add time delta to date
date = pd.to_datetime("4th of July, 2015")
date + pd.to_timedelta(np.arange(12), 'D')

# Convert date strings and python datetimes to Pandas dates
dates = pd.to_datetime([datetime(2015, 7, 3), '4th of July, 2015',
                       '2015-Jul-6', '07-07-2015', '20150708'])
dates

# Create a date range
pd.date_range('2015-07-03', '2015-07-10')

# Create a daterange of n periods with f frequency
pd.date_range('2015-07-03', periods=8, freq='6H')

# ### Date Queries

# Query Dates
mydates[mydates > np.datetime64('2015-07-11')]

index = pd.DatetimeIndex(['2014-07-04', '2014-08-04',
                          '2015-07-04', '2015-08-04'])
data = pd.Series([0, 1, 2, 3], index=index)
data

# Grab date slice
data['2014-07-04':'2015-07-04']

data['2015']

# ### Shifting











# ### Backfilling

# +
# forward/backfill missing timesteps

# +
fig, ax = plt.subplots(2, sharex=True)
data = goog.iloc[:10]

data.asfreq('D').plot(ax=ax[0], marker='o')

data.asfreq('D', method='bfill').plot(ax=ax[1], style='-o')
data.asfreq('D', method='ffill').plot(ax=ax[1], style='--o')
ax[1].legend(["back-fill", "forward-fill"]);

StarcoderdataJupyterScriptsDedupFiltered