Repository: geopandas/geopandas
Pull request #3416 (2024-08-31 07:12:53)

COMPAT: include keepdims GeometryArray._reduce
`geopandas.array.GeometryArray._reduce` issues a FutureWarning if the parameter `keepdims` is not set. `GeometryArray` inherits from `pandas.api.extensions.ExtensionArray` and its `_reduce` is overridden in `GeometryArray`.  Setting `keepdims=False` in `GeometryArray._reduce` turns off this warning. 

See issue #3403 for details.

jorisvandenbossche: @pya thanks for the PR and the reproducer! 

Based on your example, I looked a bit where the warning exactly is coming from (because it still does not make sense to get this warning, at first sight, wondering if (goe)pandas is doing something wrong). 
It seems it only happens when the masking dropped all rows (no duplicates), and you do `dropna()` on an empty geodataframe. At that point, inside that pandas method, it essentially does a `notna().all()` at some point, and it seems that `notna()` doesn't return a boolean data type for the geometry column when it is empty:

```

In [13]: gdf_empty = geopandas.GeoDataFrame({"geometry": []})

In [14]: gdf_empty.dtypes
Out[14]: 
geometry    geometry
dtype: object

In [15]: gdf_empty.nona().dtypes
Out[15]: 
geometry    geometry
dtype: object
```

This is not an error on our side, AFAIK, because the `GeometryArray.notna()` correctly returns an empty boolean array, so it seems to be a bug on the pandas side that it incorrectly preserves the original dtype for empty dataframes (will report over there).

Anyway, regardless of it being a pandas issue, we can still add this keyword to the signature (and just keep ignoring it as we already did through `kwargs`) to avoid the warning for now.


jorisvandenbossche: Ah, actually this is on our side probably, given that it depends on the name of the column ..

```python
In [21]: geopandas.GeoDataFrame({"a": []}, dtype="geometry").isna().dtypes
Out[21]: 
a    bool
dtype: object

In [22]: geopandas.GeoDataFrame({"geometry": []}, dtype="geometry").isna().dtypes
Out[22]: 
geometry    geometry
dtype: object
```
jorisvandenbossche: Opened https://github.com/geopandas/geopandas/issues/3417
pya: Nice that this rather cosmetic change helped to find a real problem.  

Patch:
diff --git a/.gitattributes b/.gitattributes
index 8340bd64..7de1e4f0 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,1 +1,3 @@
 geopandas/_version.py export-subst
+# GitHub syntax highlighting
+pixi.lock linguist-language=YAML linguist-generated=true
diff --git a/.gitignore b/.gitignore
index b2145e86..fa41cb46 100644
--- a/.gitignore
+++ b/.gitignore
@@ -72,4 +72,9 @@ geopandas/datasets/ne_110m_admin_0_countries.zip
 doc/source/getting_started/my_file.geojson
 
 .ruff_cache
-.env
\ No newline at end of file
+.env
+# pixi environments
+.pixi
+*.egg-info
+pixi.toml
+pixi.lock
diff --git a/geopandas/array.py b/geopandas/array.py
index 32f338a7..eddfd198 100644
--- a/geopandas/array.py
+++ b/geopandas/array.py
@@ -1637,7 +1637,7 @@ class GeometryArray(ExtensionArray):
         data = np.concatenate([ga._data for ga in to_concat])
         return GeometryArray(data, crs=_get_common_crs(to_concat))
 
-    def _reduce(self, name, skipna=True, **kwargs):
+    def _reduce(self, name, skipna=True, keepdims=False, **kwargs):
         # including the base class version here (that raises by default)
         # because this was not yet defined in pandas 0.23
         if name in ("any", "all"):


Test patch:
diff --git a/geopandas/tests/test_geodataframe.py b/geopandas/tests/test_geodataframe.py
index c710b262..efc7bad2 100644
--- a/geopandas/tests/test_geodataframe.py
+++ b/geopandas/tests/test_geodataframe.py
@@ -1629,3 +1629,17 @@ def test_set_geometry_supply_arraylike(dfs, geo_col_name):
         res4 = df.set_geometry(centroids, drop=True)
     assert res4.active_geometry_name == "centroids"
     assert geo_col_name in res4.columns
+
+
+@pytest.mark.filterwarnings("error::FutureWarning")
+def test_reduce_geometry_array():
+    """
+    Check for a FutureWarning.
+
+    `geopandas.array.GeometryArray._reduce` issues a FutureWarning if
+    the parameter `keepdims` is not set.
+    `GeometryArray` inherits from `pandas.api.extensions.ExtensionArray`
+    and its `_reduce` is overridden in `GeometryArray`.
+    This warning is issued with pandas 2.2.2 (tested).
+    """
+    GeoDataFrame({"geometry": []}).all()