Bondify / gtfs_functions

Package with useful functions to create geo-spatial visualizations from a GTFS.
MIT License
114 stars 30 forks source link

stop_freq error (max_trips key error) #5

Closed mmarks91 closed 9 months ago

mmarks91 commented 3 years ago

I've been using gtfs_functions to create stop_freq tables for a number of transit agencies. Stop_freq seems to work without a hitch for larger agencies, but I've had some trouble generating stop_freq for smaller agencies--I'll get a key error for max_trips. I've tried to do some workarounds by assigning new trip_ids to the stop_times file, in case the issue was the trip_id, but that hasn't seemed to work. I also tried creating a new stop_times file that doesn't have any null arrival/departure times and feeding it to stop_freq, but that hasn't worked either.

I was wondering if there's an explanation for what might cause a GTFS file to generate this error in stop_freq, and if there is any potential workaround. Thank you!

Below is the error I've received when trying to generate stop_freq:

KeyError                                  Traceback (most recent call last)
~\Anaconda3\envs\TestEnv\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'max_trips'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-156-6c38c2598be4> in <module>
      6 stop_freq4 = gtfs.stops_freq(stop_times4, stops4, cutoffs = cutoffs)
      7 stop_freq5 = gtfs.stops_freq(stop_times5, stops5, cutoffs = cutoffs)
----> 8 stop_freq6 = gtfs.stops_freq(stop_times6_fix, stops6, cutoffs = cutoffs)
      9 stop_freq_merge=pd.concat([stop_freq,stop_freq2,stop_freq3,stop_freq4,stop_freq5,stop_freq6])
     10 stop_freq_merge

~\Anaconda3\envs\TestEnv\lib\site-packages\gtfs_functions\gtfs_funtions.py in stops_freq(stop_times, stops, cutoffs)
   1132 
   1133     trips_per_hour.rename(columns={'trip_id':'max_trips'}, inplace=True)
-> 1134     trips_per_hour['max_frequency'] = (60/trips_per_hour['max_trips']).astype(int)
   1135 
   1136     max_trips = trips_per_hour.pivot_table('max_trips', index=['stop_id', 'direction_id'], aggfunc='max').reset_index()

~\Anaconda3\envs\TestEnv\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3022             if self.columns.nlevels > 1:
   3023                 return self._getitem_multilevel(key)
-> 3024             indexer = self.columns.get_loc(key)
   3025             if is_integer(indexer):
   3026                 indexer = [indexer]

~\Anaconda3\envs\TestEnv\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 'max_trips'
Bondify commented 3 years ago

hi @mmarks91 , sorry for the late reply. It is the first time I see this kind of error. Would you mind attaching the source GTFS for this code?

mmarks91 commented 3 years ago

Hi, I'm so sorry for the even later reply on my end. I ended up putting this project aside for a while but am now coming back to it.

There are a handful of GTFS files I've seen with this issue, but here is an example from MetroWest in Massachusetts. Seems like it has all the necessary files to run gtfs_functions (stops, stop_times, trips, routes, shapes). In this case, stops_freq works just fine, but line_freq generates the "max_trips" error. Would love to know what might cause this error.

Thank you!!!

On Sun, Apr 25, 2021 at 1:04 PM Santiago Toso @.***> wrote:

hi @mmarks91 https://github.com/mmarks91 , sorry for the late reply. It is the first time I see this kind of error. Would you mind attaching the source GTFS for this code?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Bondify/gtfs_functions/issues/5#issuecomment-826355912, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMMVTKKNYSK5POX5SJMRULTKRDQZANCNFSM4Z3NE6VA .

-- Michael Marks @.*** (301)-767-6553

herbertmn commented 2 years ago

Hi, I had the same issue and my problem was that the field "direction_id" in my GTFS dataset was NAN. Thus, I have tried to set it to a generic value 1, and it solved the problem. Hope it can help.