diff --git a/dask_geopandas/core.py b/dask_geopandas/core.py index 9843f315..ff883255 100644 --- a/dask_geopandas/core.py +++ b/dask_geopandas/core.py @@ -519,6 +519,11 @@ def set_geometry(self, col): meta = self._meta.set_geometry(col) return self.map_partitions(M.set_geometry, col, meta=meta) + @derived_from(geopandas.GeoDataFrame) + def rename_geometry(self, col): + meta = self._meta.rename_geometry(col) + return self.map_partitions(M.rename_geometry, col, meta=meta) + def __getitem__(self, key): """ If the result is a new dask_geopandas.GeoDataFrame/GeoSeries (automatically diff --git a/doc/source/docs/reference/geodataframe.rst b/doc/source/docs/reference/geodataframe.rst index 0021c238..0e69dcc7 100644 --- a/doc/source/docs/reference/geodataframe.rst +++ b/doc/source/docs/reference/geodataframe.rst @@ -38,6 +38,7 @@ Active geometry handling :toctree: api/ GeoDataFrame.set_geometry + GeoDataFrame.rename_geometry Aggregating and exploding ------------------------- diff --git a/tests/test_core.py b/tests/test_core.py index 8571e7e7..29886c65 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -363,6 +363,26 @@ def test_set_geometry_with_dask_geoseries(): assert_geoseries_equal(dask_obj.geometry.compute(), expected.geometry) +def test_rename_geometry(geodf_points): + df = geodf_points + dask_obj = dask_geopandas.from_geopandas(df, npartitions=2) + renamed = dask_obj.rename_geometry("points") + assert renamed._meta.geometry.name == "points" + + for part in renamed.partitions: + assert part.compute().geometry.name == "points" + + result = renamed.compute() + assert_geodataframe_equal(result, df.rename_geometry("points")) + + +def test_rename_geometry_error(geodf_points): + df = geodf_points + dask_obj = dask_geopandas.from_geopandas(df, npartitions=2) + with pytest.raises(ValueError, match="Column named value1 already exists"): + dask_obj.rename_geometry("value1") + + def test_from_dask_dataframe_with_dask_geoseries(): df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [1, 2, 3, 4]}) dask_obj = dd.from_pandas(df, npartitions=2)