# ---
# 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
# ---

# +
#this file computes the number of people using the metro between timestamps
# -

import pandas as pd
from datetime import datetime,timedelta
mta_data = pd.read_pickle('mta_data.pickle')

# +
#reset index and drop old indices to remove duplicate indexes and fix cols
mta_data = mta_data.reset_index().drop('index',axis=1)
mta_data.columns = [col.strip() for col in mta_data.columns]


# convert dates to datetime, create col; convert that to day of week, create col; convert that to weekday T/F, create col
mta_data['weekday'] = pd.to_datetime(mta_data.DATE,format='%m/%d/%Y')
mta_data['weekday_n'] = mta_data.weekday.dt.dayofweek
mta_data['weekday_tf'] = mta_data.weekday_n<5

#create columns for entries since last time
mta_data['roll_ent'] = mta_data.groupby(['weekday'])['ENTRIES'].diff(periods = 1)
mta_data['roll_ex'] = mta_data.groupby(['weekday'])['EXITS'].diff(periods = 1)
mta_data.fillna(0,inplace=True)

#find mean of data by station and whether weekday or not
mean_entry = mta_data.groupby(['STATION','weekday_tf']).mean()
#mean total traffic, then only that
mean_entry['through'] = mean_entry.ENTRIES + mean_entry.EXITS
mean_entry.drop(labels = ['ENTRIES','EXITS','weekday_n'],axis=1,inplace=True)
#shift table so that each station's through is in cols T and F, then subtract
mean_entry.reset_index()
piv_me=mean_entry.pivot_table(index='STATION',columns='weekday_tf')
weekdaymean = piv_me['through']['weekday_tf'==False]
weekendmean = piv_me['through']['weekday_tf'==True]
weekdaymean-weekendmean


# -

def find_growth(dataframe,column) :
    at_time = []
    at_time.append(0)
    for i in range(1,len(dataframe)) :
        if dataframe.iloc[i]['weekday']==dataframe.iloc[i-1]['weekday'] :
            at_time.append(dataframe.iloc[i][column]-dataframe.iloc[i-1][column])
        else :
            at_time.append(0)
    return pd.Series(at_time)


mta_data.columns = [col.strip() for col in mta_data.columns]

mta_data['roll_ent'] = mta_data.groupby(['weekday'])['ENTRIES'].diff(periods = 1)
mta_data['roll_ex'] = mta_data.groupby(['weekday'])['EXITS'].diff(periods = 1)
mta_data.fillna(0,inplace=True)

# +
mean_entry = mta_data.groupby(['STATION','weekday_tf']).mean()
mean_entry['through'] = mean_entry.roll_ent + mean_entry.roll_ex
mean_entry.drop(labels = ['ENTRIES','EXITS','weekday_n','roll_ent','roll_ex'],axis=1,inplace=True)
mean_entry.reset_index()
piv_me=mean_entry.pivot_table(index='STATION',columns='weekday_tf')

weekdaymean = piv_me['through']['weekday_tf'==False].map(int)
weekendmean = piv_me['through']['weekday_tf'==True].map(int)
piv_me.head(5)
mta_data[mta_data.roll_ent<0]
mta_data.iloc[40:50]

# +
from datetime import datetime,timedelta
#reset index and drop old indices to remove duplicate indexes and fix cols
mta_data = mta_data.reset_index().drop('index',axis=1)
mta_data.columns = [col.strip() for col in mta_data.columns]


# convert dates to datetime, create col; convert that to day of week, create col; convert that to weekday T/F, create col
mta_data['weekday'] = pd.to_datetime(mta_data.DATE,format='%m/%d/%Y')
mta_data['weekday_n'] = mta_data.weekday.dt.dayofweek
mta_data['weekday_tf'] = mta_data.weekday_n<5

#create columns for entries since last time
mta_data['roll_ent'] = mta_data.groupby(['weekday'])['ENTRIES'].diff(periods = 1)
mta_data['roll_ex'] = mta_data.groupby(['weekday'])['EXITS'].diff(periods = 1)
mta_data.fillna(0,inplace=True)

#find mean of data by station and whether weekday or not
mean_entry = mta_data.groupby(['STATION','weekday_tf']).mean()
#mean total traffic, then only that
mean_entry['through'] = mean_entry.ENTRIES + mean_entry.EXITS
mean_entry.drop(labels = ['ENTRIES','EXITS','weekday_n'],axis=1,inplace=True)
#shift table so that each station's through is in cols T and F, then subtract
mean_entry.reset_index()
piv_me=mean_entry.pivot_table(index='STATION',columns='weekday_tf')
weekdaymean = piv_me['through']['weekday_tf'==False]
weekendmean = piv_me['through']['weekday_tf'==True]
weekdaymean-weekendmean
# -



mta_data['roll_ent'] = mta_data.groupby(['weekday'])['ENTRIES'].diff(periods = 1)
mta_data['roll_ex'] = mta_data.groupby(['weekday'])['EXITS'].diff(periods = 1)

mta_data.head(10)

mta_data.iloc[40:50]

mta_data[mta_data.roll_ent<0]



StarcoderdataJupyterScriptsDedupFiltered