Skip to content

Commit cc60fa2

Browse files
m-richardsjorisvandenbossche
authored andcommitted
resolve apply pandas 2.1 deprecation warning from default val
pandas-dev/pandas#52786
1 parent d8b9a4a commit cc60fa2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

geopandas/geoseries.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,14 @@ def select(self, *args, **kwargs):
647647
return self._wrapped_pandas_method("select", *args, **kwargs)
648648

649649
@doc(pd.Series)
650-
def apply(self, func, convert_dtype: bool = True, args=(), **kwargs):
651-
result = super().apply(func, convert_dtype=convert_dtype, args=args, **kwargs)
650+
def apply(self, func, convert_dtype: bool = None, args=(), **kwargs):
651+
if convert_dtype is not None:
652+
kwargs["convert_dtype"] = convert_dtype
653+
elif convert_dtype is None and not compat.PANDAS_GE_21:
654+
kwargs["convert_dtype"] = True
655+
# if compat.PANDAS_GE_21 don't pass through, use pandas default -> true
656+
# to avoid warning
657+
result = super().apply(func, args=args, **kwargs)
652658
if isinstance(result, GeoSeries):
653659
if self.crs is not None:
654660
result.set_crs(self.crs, inplace=True)

geopandas/tests/test_pandas_methods.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import os
2+
<<<<<<< HEAD
23
from packaging.version import Version
4+
=======
5+
import warnings
6+
>>>>>>> 5be79c17... resolve apply pandas 2.1 deprecation warning from default val
37

48
import numpy as np
59
from numpy.testing import assert_array_equal
@@ -710,6 +714,14 @@ def numeric_func(geom):
710714
assert not isinstance(result, GeoSeries)
711715
assert_series_equal(result, pd.Series([0.0, 1.0, 2.0]))
712716

717+
with warnings.catch_warnings(record=True) as record:
718+
s.apply(lambda x: x.centroid, convert_dtype=False)
719+
if compat.PANDAS_GE_21:
720+
assert len(record) == 1
721+
assert "the convert_dtype parameter" in str(record[0].message)
722+
else:
723+
assert len(record) == 0
724+
713725

714726
def test_apply_loc_len1(df):
715727
# subset of len 1 with loc -> bug in pandas with inconsistent Block ndim

0 commit comments

Comments
 (0)