Skip to content

Commit b8b6c79

Browse files
Fixed dropping the geometry column (#322)
1 parent 105bfd6 commit b8b6c79

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Packaging:
99
- `dask>=2025.1.0` is now required.
1010
- `python>=3.10` is now required.
1111

12+
Bug fixes:
13+
14+
- Fixed `GeoDataFrame.drop` returning a `GeoDataFrame`
15+
instead of a `DataFrame`, when dropping the geometry
16+
column (#321).
1217

1318
Version 0.4.2 (September 24, 2024)
1419
----------------------------------

dask_geopandas/_expr.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import Literal
2+
3+
import dask.dataframe.dask_expr as dx
4+
5+
import geopandas
6+
7+
8+
def _drop(df: geopandas.GeoDataFrame, columns, errors):
9+
return df.drop(columns=columns, errors=errors)
10+
11+
12+
def _validate_axis(axis=0, none_is_zero: bool = True) -> None | Literal[0, 1]:
13+
if axis not in (0, 1, "index", "columns", None):
14+
raise ValueError(f"No axis named {axis}")
15+
# convert to numeric axis
16+
numeric_axis: dict[str | None, Literal[0, 1]] = {"index": 0, "columns": 1}
17+
if none_is_zero:
18+
numeric_axis[None] = 0
19+
20+
return numeric_axis.get(axis, axis)
21+
22+
23+
class Drop(dx.expr.Drop):
24+
operation = staticmethod(_drop)

dask_geopandas/expr.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import dask_geopandas
2828

29+
from ._expr import Drop, _validate_axis
2930
from .geohash import _geohash
3031
from .hilbert_distance import _hilbert_distance
3132
from .morton_distance import _morton_distance
@@ -868,6 +869,25 @@ def explode(self, column=None, ignore_index=False, index_parts=None):
868869
enforce_metadata=False,
869870
)
870871

872+
@derived_from(geopandas.GeoDataFrame)
873+
def drop(self, labels=None, axis=0, columns=None, errors="raise"):
874+
# https://github.com/geopandas/dask-geopandas/issues/321
875+
# Override to avoid an inplace drop, since we need
876+
# to convert from a GeoDataFrame to a DataFrame when dropping
877+
# the geometry column.
878+
if columns is None and labels is None:
879+
raise TypeError("must either specify 'columns' or 'labels'")
880+
881+
axis = _validate_axis(axis)
882+
883+
if axis == 1:
884+
columns = labels or columns
885+
elif axis == 0 and columns is None:
886+
raise NotImplementedError(
887+
"Drop currently only works for axis=1 or when columns is not None"
888+
)
889+
return new_collection(Drop(self, columns=columns, errors=errors))
890+
871891

872892
from_geopandas = dd.from_pandas
873893

dask_geopandas/tests/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,21 @@ def get_chunk(n):
10341034
assert_geodataframe_equal(ddf.compute(), expected)
10351035

10361036

1037+
def test_drop():
1038+
# https://github.com/geopandas/dask-geopandas/issues/321
1039+
df = dask_geopandas.from_geopandas(
1040+
geopandas.GeoDataFrame({"col": [1], "geometry": [Point(1, 1)]}), npartitions=1
1041+
)
1042+
result = df.drop(columns="geometry")
1043+
assert type(result) is dd.DataFrame
1044+
1045+
result = df.drop(columns="col")
1046+
assert type(result) is dask_geopandas.GeoDataFrame
1047+
1048+
with pytest.raises(ValueError, match="No axis named x"):
1049+
df.drop(labels="a", axis="x")
1050+
1051+
10371052
def test_core_deprecated():
10381053
with pytest.warns(FutureWarning, match="dask_geopandas.core"):
10391054
import dask_geopandas.core # noqa: F401

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ section-order = [
155155
]
156156

157157
[tool.ruff.lint.isort.sections]
158-
"dask" = ["dask", "dask_expr"]
158+
"dask" = ["dask"]
159159
"geo" = ["geopandas", "shapely", "pyproj"]
160160
"testing" = ["pytest", "pandas.testing", "numpy.testing", "geopandas.tests", "geopandas.testing"]

0 commit comments

Comments
 (0)