Skip to content

Commit

Permalink
Merge pull request #16 from kuanb/kuanb-interpolate-df-reset-index
Browse files Browse the repository at this point in the history
Prevent index name collision on index reset for interpolated df
  • Loading branch information
pksohn authored Apr 21, 2017
2 parents e13406e + cd25462 commit 6a48626
Showing 1 changed file with 18 additions and 2 deletions.
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)

0 comments on commit 6a48626

Please sign in to comment.