Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: reducing shapely ufunc failing when called on a GeoSeries #3200

Open
jorisvandenbossche opened this issue Feb 28, 2024 · 0 comments
Open

Comments

@jorisvandenbossche
Copy link
Member

Not necessarily an issue in the geopandas code itself, but just opening it here for now to track it somewhere. I ran into this in geopandas/dask-geopandas#268

Calling shapely.geometrycollections on a GeometryArray works fine (it returns a single shapely GeometryCollection geometry object), but calling the same on the equivalent GeoSeries fails:

In [2]: import geopandas

In [3]: arr = geopandas.points_from_xy([0, 1, 2], [1,2,3])

In [4]: arr
Out[4]: 
<GeometryArray>
[<POINT (0 1)>, <POINT (1 2)>, <POINT (2 3)>]
Length: 3, dtype: geometry

In [5]: import shapely

In [6]: shapely.geometrycollections(arr)
Out[6]: <GEOMETRYCOLLECTION (POINT (0 1), POINT (1 2), POINT (2 3))>

In [7]: shapely.geometrycollections(geopandas.GeoSeries(arr))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 shapely.geometrycollections(geopandas.GeoSeries(arr))

File ~/conda/envs/geo-dev/lib/python3.11/site-packages/shapely/decorators.py:77, in multithreading_enabled.<locals>.wrapped(*args, **kwargs)
     75     for arr in array_args:
     76         arr.flags.writeable = False
---> 77     return func(*args, **kwargs)
     78 finally:
     79     for arr, old_flag in zip(array_args, old_flags):

File ~/conda/envs/geo-dev/lib/python3.11/site-packages/shapely/creation.py:460, in geometrycollections(geometries, indices, out, **kwargs)
    458 typ = GeometryType.GEOMETRYCOLLECTION
    459 if indices is None:
--> 460     return lib.create_collection(geometries, typ, out=out, **kwargs)
    461 else:
    462     return collections_1d(geometries, indices, typ, out=out)

File ~/conda/envs/geo-dev/lib/python3.11/site-packages/pandas/core/generic.py:2168, in NDFrame.__array_ufunc__(self, ufunc, method, *inputs, **kwargs)
   2164 @final
   2165 def __array_ufunc__(
   2166     self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any
   2167 ):
-> 2168     return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)

File ~/conda/envs/geo-dev/lib/python3.11/site-packages/pandas/core/arraylike.py:414, in array_ufunc(self, ufunc, method, *inputs, **kwargs)
    411         result = default_array_ufunc(inputs[0], ufunc, method, *inputs, **kwargs)
    412         # e.g. np.negative (only one reached), with "where" and "out" in kwargs
--> 414 result = reconstruct(result)
    415 return result

File ~/conda/envs/geo-dev/lib/python3.11/site-packages/pandas/core/arraylike.py:343, in array_ufunc.<locals>.reconstruct(result)
    339 if ufunc.nout > 1:
    340     # np.modf, np.frexp, np.divmod
    341     return tuple(_reconstruct(x) for x in result)
--> 343 return _reconstruct(result)

File ~/conda/envs/geo-dev/lib/python3.11/site-packages/pandas/core/arraylike.py:349, in array_ufunc.<locals>._reconstruct(result)
    346 if lib.is_scalar(result):
    347     return result
--> 349 if result.ndim != self.ndim:
    350     if method == "outer":
    351         raise NotImplementedError

AttributeError: 'GeometryCollection' object has no attribute 'ndim'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant