Skip to content

Commit

Permalink
ENH: implement rename_geometry (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis authored Feb 11, 2022
1 parent a978e2d commit 916e592
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dask_geopandas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/source/docs/reference/geodataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Active geometry handling
:toctree: api/

GeoDataFrame.set_geometry
GeoDataFrame.rename_geometry

Aggregating and exploding
-------------------------
Expand Down
20 changes: 20 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 916e592

Please sign in to comment.