Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: implement rename_geometry #157

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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