REGR: fix bug in apply when returning a series (#2286)

This commit is contained in:
Matt Richards
2022-01-20 10:18:46 +01:00
committed by GitHub
parent 4346120b5e
commit cebb5873d0
2 changed files with 13 additions and 1 deletions
+4 -1
View File
@@ -1408,7 +1408,10 @@ individually so that features may have different properties
func, axis=axis, raw=raw, result_type=result_type, args=args, **kwargs
)
# Reconstruct gdf if it was lost by apply
if self._geometry_column_name in result.columns:
if (
isinstance(result, DataFrame)
and self._geometry_column_name in result.columns
):
# axis=1 apply will split GeometryDType to object, try and cast back
try:
result = result.set_geometry(self._geometry_column_name)
+9
View File
@@ -652,6 +652,15 @@ def test_apply_preserves_geom_col_name(df):
assert result.geometry.name == "geom"
def test_df_apply_returning_series(df):
# https://github.com/geopandas/geopandas/issues/2283
result = df.apply(lambda row: row.geometry, axis=1)
assert_geoseries_equal(result, df.geometry, check_crs=False)
result = df.apply(lambda row: row.value1, axis=1)
assert_series_equal(result, df["value1"].rename(None))
@pytest.mark.skipif(not compat.PANDAS_GE_10, reason="attrs introduced in pandas 1.0")
def test_preserve_attrs(df):
# https://github.com/geopandas/geopandas/issues/1654