Skip to content

Commit 0d41c8a

Browse files
committed
Fixed dropping the geometry column
1 parent f629462 commit 0d41c8a

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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_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 = dx.from_pandas
873893

dask_geopandas/tests/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,3 +1046,15 @@ def get_chunk(n):
10461046

10471047
expected = geopandas.GeoDataFrame({"col": [1, 1], "geometry": [Point(1, 1)] * 2})
10481048
assert_geodataframe_equal(ddf.compute(), expected)
1049+
1050+
1051+
def test_drop():
1052+
# https://github.com/geopandas/dask-geopandas/issues/321
1053+
df = dask_geopandas.from_geopandas(
1054+
geopandas.GeoDataFrame({"col": [1], "geometry": [Point(1, 1)]}), npartitions=1
1055+
)
1056+
result = df.drop(columns="geometry")
1057+
assert type(result) is dd.DataFrame
1058+
1059+
result = df.drop(columns="col")
1060+
assert type(result) is dask_geopandas.GeoDataFrame

0 commit comments

Comments
 (0)