Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent index name collision on index reset for interpolated df #16

Merged
merged 4 commits into from
Apr 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions urbanaccess/gtfs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,18 @@ def interpolatestoptimes(stop_times_df, calendar_selected_trips_df, day):
df_for_interpolation['stop_sequence_merge'] = (
df_for_interpolation[~trailing]['stop_sequence'])

# Need to check if existing index in column names and drop if so (else
# a ValueError where Pandas can't insert b/c col already exists will occur)
drop_bool = False
if _check_if_index_name_in_cols(df_for_interpolation):
# move the current index to own col named 'index'
col_name_to_copy = df_for_interpolation.index.name
col_to_copy = df_for_interpolation[col_name_to_copy].copy()
df_for_interpolation['index'] = col_to_copy
drop_bool = True
df_for_interpolation.reset_index(inplace=True, drop=drop_bool)

# Merge back into original index
df_for_interpolation.reset_index(inplace=True)
interpolated_df = pd.merge(df_for_interpolation, melted, 'left',
on=['stop_sequence_merge', 'unique_trip_id'])
interpolated_df.set_index('index', inplace=True)
Expand Down Expand Up @@ -763,4 +773,10 @@ def load_processed_gtfs_data(dir=config.settings.data_folder,filename=None):
if 'calendar_dates' in store.keys():
gtfsfeeds_df.calendar_dates = hdf5_to_df(dir=dir,filename=filename,key='calendar_dates')

return gtfsfeeds_df
return gtfsfeeds_df

# helper functions
def _check_if_index_name_in_cols(df):
cols = df.columns.values
iname = df.index.name
return (iname in cols)