<jupyter_start><jupyter_text>SummaryDo I sleep less on nights when the Warriors play?<jupyter_code>import pandas as pd
import os
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import nba_py

sns.set_context('poster')
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
data_path = os.path.join(os.getcwd(), os.pardir, 'data', 'interim', 'sleep_data.csv')
df_sleep = pd.read_csv(data_path, index_col='shifted_datetime', parse_dates=True)
df_sleep.index += pd.Timedelta(hours=12)
sleep_day = df_sleep.resample('1D').sum().fillna(0)
from nba_py import league
gswlog = league.GameLog(player_or_team='T')
league_logs = gswlog.json['resultSets'][0]['rowSet']
columns = gswlog.json['resultSets'][0]['headers']
df_league = pd.DataFrame(league_logs, columns=columns)
df_league.columns
gsw_games = df_league[df_league['TEAM_ABBREVIATION'] == 'GSW']
len(gsw_games)
gsw_games.head()
gsw_dates = gsw_games['GAME_DATE']
toplot = sleep_day['minutesAsleep']/60.

data = []

data.append(
    go.Scatter(
        x=toplot.index,
        y=toplot.values,
        name='Hours Asleep'
    )
)

shapes = []
for idate, gsw_date in enumerate(gsw_dates):
    if idate == 0:
        showlegend = True
    else:
        showlegend = False
    trace0 = go.Scatter(
        x=[gsw_date],
        y=[toplot.dropna().min()],
        mode='markers',
        name='Golden State Warriors Game',
        marker=dict(
            color='salmon'
        ),
        showlegend=showlegend
    )
    data.append(trace0)

layout = go.Layout(
    title="Daily Sleep Total, 6pm to 6pm",
    yaxis=dict(
        title='Hours Asleep'
    ),
)

fig = {
    'data': data,
    'layout': layout,
}

py.iplot(fig, filename='DailySleepTotal_GSWGames')<jupyter_output><empty_output><jupyter_text>The NBA season starts at the end of October. I got my fitbit near the beginning of November, so there is a lot of overlap. A simple test: sleep on the night of a Warriors game vs. all other nights. > Here, we have to be careful about the definition of a night of sleep. I follow Fitbit's convention and assert that hours of sleep on a given date correspond to falling asleep the night before and waking up the day of. So if I fall asleep on Tuesday August 8th at 11pm and wake up on Wednesday August 9th at 7am, then I got 8 hours of sleep on August 9th.To answer my question about sleep on the night of a Warriors game, I need to compare sleep the day AFTER the Warriors game to all other nights. E.g., the Warriors had a game on April 8, 2017. The relevant night of sleep for that game is April 9, 2017. A quick and dirty way to tackle this is by adding 24 hours to the Warriors game dates.<jupyter_code>gsw_dates = pd.to_datetime(gsw_dates)
gswdatedf = pd.DataFrame(index=gsw_dates)
gswdatedf['game_status'] = 1
gswdatedf = gswdatedf.resample('1D').sum().fillna(0)
gswdatedf_next = gswdatedf.copy()
gswdatedf_next.index += pd.Timedelta(hours=24)
sleepgsw = sleep_day.join(gswdatedf_next, how='inner')
sleepgswyes = sleepgsw.groupby('game_status').mean()
sleepgswyes['minutesAsleep'] / 60<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10558213716108453, 'fraction_numerical': 0.012440191387559809, 'mean_word_length': 4.3062605752961085, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '299183', 'license': 'MIT', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1055, 'n_words': 321, 'path': 'notebooks/sbussmann_data-nba.ipynb', 'repo_name': 'sbussmann/sleep-b', 'size': '3135'}