Bondify / gtfs_functions

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

Passing list-likes to .loc or [] with any missing labels is no longer supported. #7

Closed christopherusky closed 2 years ago

christopherusky commented 3 years ago

Running:

routes, stops, stop_times, trips, shapes = gtfs.import_gtfs(r"itm_north_east_gtfs.zip")

Gives:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-17-8b3c4719a61b> in <module>
----> 1 routes, stops, stop_times, trips, shapes = gtfs.import_gtfs(r"itm_north_east_gtfs.zip")

~/opt/anaconda3/lib/python3.8/site-packages/gtfs_functions/gtfs_funtions.py in import_gtfs(gtfs_path, busiest_date)
     89 
     90     # Get routes info in trips
---> 91     trips = pd.merge(trips, routes, how='left').loc[:, ['trip_id', 'route_id',
     92                                                         'service_id', 'direction_id','shape_id']]
     93 

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in __getitem__(self, key)
    871                     # AttributeError for IntervalTree get_value
    872                     pass
--> 873             return self._getitem_tuple(key)
    874         else:
    875             # we by definition only have the 0th axis

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1053             return self._multi_take(tup)
   1054 
-> 1055         return self._getitem_tuple_same_dim(tup)
   1056 
   1057     def _get_label(self, label, axis: int):

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_tuple_same_dim(self, tup)
    748                 continue
    749 
--> 750             retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
    751             # We should never have retval.ndim < self.ndim, as that should
    752             #  be handled by the _getitem_lowerdim call above.

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1097                     raise ValueError("Cannot index with multidimensional key")
   1098 
-> 1099                 return self._getitem_iterable(key, axis=axis)
   1100 
   1101             # nested tuple slicing

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_iterable(self, key, axis)
   1035 
   1036         # A collection of keys
-> 1037         keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
   1038         return self.obj._reindex_with_indexers(
   1039             {axis: [keyarr, indexer]}, copy=True, allow_dups=True

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1252             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1253 
-> 1254         self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
   1255         return keyarr, indexer
   1256 

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1313 
   1314                 with option_context("display.max_seq_items", 10, "display.width", 80):
-> 1315                     raise KeyError(
   1316                         "Passing list-likes to .loc or [] with any missing labels "
   1317                         "is no longer supported. "

KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Index(['direction_id'], dtype='object'). See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike"

.iloc for lists with any missing labels dropped from Pandas 1.0.0 onwards, fix is to use. Pandas tells that:

In prior versions, using .loc[list-of-labels] would work as long as at least 1 of the keys was found (otherwise it would raise a KeyError). This behavior was changed and will now raise a KeyError if at least one label is missing. The recommended alternative is to use .reindex().

Will have a look to see what is needed to implement.