Skip to content

Commit cebb587

Browse files
authored
REGR: fix bug in apply when returning a series (#2286)
1 parent 4346120 commit cebb587

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

geopandas/geodataframe.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,10 @@ def apply(self, func, axis=0, raw=False, result_type=None, args=(), **kwargs):
14081408
func, axis=axis, raw=raw, result_type=result_type, args=args, **kwargs
14091409
)
14101410
# Reconstruct gdf if it was lost by apply
1411-
if self._geometry_column_name in result.columns:
1411+
if (
1412+
isinstance(result, DataFrame)
1413+
and self._geometry_column_name in result.columns
1414+
):
14121415
# axis=1 apply will split GeometryDType to object, try and cast back
14131416
try:
14141417
result = result.set_geometry(self._geometry_column_name)

geopandas/tests/test_pandas_methods.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,15 @@ def test_apply_preserves_geom_col_name(df):
652652
assert result.geometry.name == "geom"
653653

654654

655+
def test_df_apply_returning_series(df):
656+
# https://github.com/geopandas/geopandas/issues/2283
657+
result = df.apply(lambda row: row.geometry, axis=1)
658+
assert_geoseries_equal(result, df.geometry, check_crs=False)
659+
660+
result = df.apply(lambda row: row.value1, axis=1)
661+
assert_series_equal(result, df["value1"].rename(None))
662+
663+
655664
@pytest.mark.skipif(not compat.PANDAS_GE_10, reason="attrs introduced in pandas 1.0")
656665
def test_preserve_attrs(df):
657666
# https://github.com/geopandas/geopandas/issues/1654

0 commit comments

Comments
 (0)