Skip to content

Commit d0257e3

Browse files
hoxbroianthomas23
andauthored
Support Pandas 2.1 (#125)
* Fix recursion error * Fix AttributeError * Remove temporary pandas < 2.1 pin --------- Co-authored-by: Ian Thomas <[email protected]>
1 parent f42dc15 commit d0257e3

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
'dask',
3535
'fsspec',
3636
'numba',
37-
'pandas <2.1',
37+
'pandas',
3838
'param',
3939
'pyarrow >=1.0',
4040
'retrying',

spatialpandas/geodataframe.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class GeoDataFrame(pd.DataFrame):
1818
# properties to propagate
1919
_metadata = ['_geometry']
2020

21+
# In Pandas 2.1 will raise AttributeError.
22+
# AttributeError: 'GeoDataFrame' object has no attribute '_geometry'
23+
# Ref: https://github.com/pandas-dev/pandas/issues/51280
24+
_geometry = None
25+
2126
def __init__(self, data=None, index=None, geometry=None, **kwargs):
2227
# Call pandas constructor, always copy
2328
kwargs.pop("copy", None)

spatialpandas/geoseries.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pandas as pd
2+
from packaging.version import Version
23

34
from .geometry import GeometryDtype, Geometry
45

@@ -34,6 +35,26 @@ def __init__(self, data, index=None, name=None, dtype=None, **kwargs):
3435
data = to_geometry_array(data, dtype)
3536
super().__init__(data, index=index, name=name, **kwargs)
3637

38+
def _constructor_from_mgr(self, mgr, axes):
39+
if Version(pd.__version__) < Version('2.1'):
40+
return super()._constructor_from_mgr(mgr, axes)
41+
42+
# Copied from pandas.Series._constructor_from_mgr
43+
# https://github.com/pandas-dev/pandas/blob/80a1a8bc3e07972376284ffce425a2abd1e58604/pandas/core/series.py#L582-L590
44+
# Changed if statement to use GeoSeries instead of Series.
45+
# REF: https://github.com/pandas-dev/pandas/pull/52132
46+
# REF: https://github.com/pandas-dev/pandas/pull/53871
47+
48+
ser = self._from_mgr(mgr, axes=axes)
49+
ser._name = None # caller is responsible for setting real name
50+
51+
if type(self) is GeoSeries: # Changed line
52+
# fastpath avoiding constructor call
53+
return ser
54+
else:
55+
assert axes is mgr.axes
56+
return self._constructor(ser, copy=False)
57+
3758
@property
3859
def _constructor(self):
3960
return _MaybeGeoSeries

0 commit comments

Comments
 (0)