Skip to content

Commit

Permalink
set index if _check_if_index_name_in_cols true to ensure no snag on l…
Browse files Browse the repository at this point in the history
…ine 306
  • Loading branch information
kuanb committed Apr 20, 2017
1 parent 69c7e8c commit b7c32d6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions urbanaccess/gtfs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ def interpolatestoptimes(stop_times_df, calendar_selected_trips_df, day):

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

Expand All @@ -303,9 +306,19 @@ def interpolatestoptimes(stop_times_df, calendar_selected_trips_df, day):
interpolated_df.set_index('index', inplace=True)
interpolated_times = interpolated_df[['departure_time_sec_interpolate']]

final_stop_times_df = pd.merge(stop_times_df, interpolated_times,
how='left', left_index=True,
right_index=True, sort=False, copy=False)
# default value for final_stop_times
final_stop_times_df = stop_times_df

# if empty just duplicate departure_time_sec col
if interpolated_times.empty:
departures = final_stop_times_df['departure_time_sec'].copy()
final_stop_times_df['departure_time_sec_interpolate'] = departures

# if df not empty, override the default final_stop_times with merge result
else:
final_stop_times_df = pd.merge(stop_times_df, interpolated_times,
how='left', left_index=True,
right_index=True, sort=False, copy=False)

# fill in nulls in interpolated departure time column using trips that did not need interpolation in order to create
# one column with both original and interpolated times
Expand Down

0 comments on commit b7c32d6

Please sign in to comment.