gboeing / osmnx-examples

Gallery of OSMnx tutorials, usage examples, and feature demonstations.
https://osmnx.readthedocs.io
MIT License
1.5k stars 519 forks source link

TypeError: “Set type is unordered” in isochrones example with Pandas 1.1 #36

Closed oefe closed 3 years ago

oefe commented 3 years ago

After upgrading to Pandas 1.1, the last cell of 13-isolines-isochrones.ipynb example fails with a TypeError: "Set type is unordered" (see Traceback below)

This worked as expected with Pandas 1.0.5, but fails with Pandas 1.1 or 1.1.1

Root cause is that this code initializes a data frame with subgraph.nodes():

nodes_gdf = gpd.GeoDataFrame({'id': subgraph.nodes()}, geometry=node_points)

subgraph.nodes() is a NodeView, which behaves both like a dictionary and a set. These are unordered types, but Pandas needs an ordered collection such as a numpy array or list. Pandas 1.1 introduced a type check to catch this in issue 32582.

Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
     26     return isochrone_polys
     27 
---> 28 isochrone_polys = make_iso_polys(G, edge_buff=25, node_buff=0, infill=True)
     29 fig, ax = ox.plot_graph(G, show=False, close=False, edge_color='#999999', edge_alpha=0.2, node_size=0)
     30 for polygon, fc in zip(isochrone_polys, iso_colors):

 in make_iso_polys(G, edge_buff, node_buff, infill)
      5 
      6         node_points = [Point((data['x'], data['y'])) for node, data in subgraph.nodes(data=True)]
----> 7         nodes_gdf = gpd.GeoDataFrame({'id': subgraph.nodes()}, geometry=node_points)
      8         nodes_gdf = nodes_gdf.set_index('id')
      9 

~/miniconda3/envs/osmnx-examples/lib/python3.8/site-packages/geopandas/geodataframe.py in __init__(self, *args, **kwargs)
     87         crs = kwargs.pop("crs", None)
     88         geometry = kwargs.pop("geometry", None)
---> 89         super(GeoDataFrame, self).__init__(*args, **kwargs)
     90 
     91         # need to set this before calling self['geometry'], because

~/miniconda3/envs/osmnx-examples/lib/python3.8/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    466 
    467         elif isinstance(data, dict):
--> 468             mgr = init_dict(data, index, columns, dtype=dtype)
    469         elif isinstance(data, ma.MaskedArray):
    470             import numpy.ma.mrecords as mrecords

~/miniconda3/envs/osmnx-examples/lib/python3.8/site-packages/pandas/core/internals/construction.py in init_dict(data, index, columns, dtype)
    281             arr if not is_datetime64tz_dtype(arr) else arr.copy() for arr in arrays
    282         ]
--> 283     return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    284 
    285 

~/miniconda3/envs/osmnx-examples/lib/python3.8/site-packages/pandas/core/internals/construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype, verify_integrity)
     81 
     82         # don't force copy because getting jammed in an ndarray anyway
---> 83         arrays = _homogenize(arrays, index, dtype)
     84 
     85         columns = ensure_index(columns)

~/miniconda3/envs/osmnx-examples/lib/python3.8/site-packages/pandas/core/internals/construction.py in _homogenize(data, index, dtype)
    349                     val = dict(val)
    350                 val = lib.fast_multiget(val, oindex._values, default=np.nan)
--> 351             val = sanitize_array(
    352                 val, index, dtype=dtype, copy=False, raise_cast_failure=False
    353             )

~/miniconda3/envs/osmnx-examples/lib/python3.8/site-packages/pandas/core/construction.py in sanitize_array(data, index, dtype, copy, raise_cast_failure)
    450         subarr = _try_cast(arr, dtype, copy, raise_cast_failure)
    451     elif isinstance(data, abc.Set):
--> 452         raise TypeError("Set type is unordered")
    453     elif lib.is_scalar(data) and index is not None and dtype is not None:
    454         data = maybe_cast_to_datetime(data, dtype)

TypeError: Set type is unordered
gboeing commented 3 years ago

closed in #37