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

Drop duplicates over multiple dims, and add Dataset.drop_duplicates #6307

Merged
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
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Dataset contents
Dataset.swap_dims
Dataset.expand_dims
Dataset.drop_vars
Dataset.drop_duplicates
Dataset.drop_dims
Dataset.set_coords
Dataset.reset_coords
Expand Down
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ New Features
- Enbable to provide more keyword arguments to `pydap` backend when reading
OpenDAP datasets (:issue:`6274`).
By `Jonas Gliß <https://github.com/jgliss>`.

- Allow :py:meth:`DataArray.drop_duplicates` to drop duplicates along multiple dimensions at once,
and add :py:meth:`Dataset.drop_duplicates`. (:pull:`6307`)
By `Tom Nicholas <https://github.com/TomNicholas>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
17 changes: 10 additions & 7 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4659,14 +4659,15 @@ def curvefit(

def drop_duplicates(
self,
dim: Hashable,
keep: (str | bool) = "first",
dim: Hashable | Iterable[Hashable] | ...,
keep: Literal["first", "last"] | Literal[False] = "first",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a breaking change to put this behind a kwarg-only separator, but probably we should do it at some point. Someone is going to have a dim called last and be confused why da.drop_duplicates("foo", "last") is not dropping things on the last dim...

):
"""Returns a new DataArray with duplicate dimension values removed.

Parameters
----------
dim : dimension label, optional
dim : dimension label or labels
Pass `...` to drop duplicates along all dimensions.
keep : {"first", "last", False}, default: "first"
Determines which duplicates (if any) to keep.
- ``"first"`` : Drop duplicates except for the first occurrence.
Expand All @@ -4676,11 +4677,13 @@ def drop_duplicates(
Returns
-------
DataArray

See Also
--------
Dataset.drop_duplicates
"""
if dim not in self.dims:
raise ValueError(f"'{dim}' not found in dimensions")
indexes = {dim: ~self.get_index(dim).duplicated(keep=keep)}
return self.isel(indexes)
deduplicated = self._to_temp_dataset().drop_duplicates(dim, keep=keep)
return self._from_temp_dataset(deduplicated)

def convert_calendar(
self,
Expand Down
39 changes: 39 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7770,6 +7770,45 @@ def _wrapper(Y, *coords_, **kwargs):

return result

def drop_duplicates(
self,
dim: Hashable | Iterable[Hashable] | ...,
keep: Literal["first", "last"] | Literal[False] = "first",
):
"""Returns a new Dataset with duplicate dimension values removed.

Parameters
----------
dim : dimension label or labels
Pass `...` to drop duplicates along all dimensions.
keep : {"first", "last", False}, default: "first"
Determines which duplicates (if any) to keep.
- ``"first"`` : Drop duplicates except for the first occurrence.
- ``"last"`` : Drop duplicates except for the last occurrence.
- False : Drop all duplicates.

Returns
-------
Dataset

See Also
--------
DataArray.drop_duplicates
"""
if isinstance(dim, str):
dims = (dim,)
elif dim is ...:
dims = self.dims
else:
dims = dim

missing_dims = set(dims) - set(self.dims)
if missing_dims:
raise ValueError(f"'{missing_dims}' not found in dimensions")

indexes = {dim: ~self.get_index(dim).duplicated(keep=keep) for dim in dims}
return self.isel(indexes)

def convert_calendar(
self,
calendar: str,
Expand Down
59 changes: 42 additions & 17 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6618,25 +6618,50 @@ def test_clip(da):
result = da.clip(min=da.mean("x"), max=da.mean("a").isel(x=[0, 1]))


@pytest.mark.parametrize("keep", ["first", "last", False])
def test_drop_duplicates(keep):
ds = xr.DataArray(
[0, 5, 6, 7], dims="time", coords={"time": [0, 0, 1, 2]}, name="test"
)
class TestDropDuplicates:
@pytest.mark.parametrize("keep", ["first", "last", False])
def test_drop_duplicates_1d(self, keep):
da = xr.DataArray(
[0, 5, 6, 7], dims="time", coords={"time": [0, 0, 1, 2]}, name="test"
)

if keep == "first":
data = [0, 6, 7]
time = [0, 1, 2]
elif keep == "last":
data = [5, 6, 7]
time = [0, 1, 2]
else:
data = [6, 7]
time = [1, 2]
if keep == "first":
data = [0, 6, 7]
time = [0, 1, 2]
elif keep == "last":
data = [5, 6, 7]
time = [0, 1, 2]
else:
data = [6, 7]
time = [1, 2]

expected = xr.DataArray(data, dims="time", coords={"time": time}, name="test")
result = da.drop_duplicates("time", keep=keep)
assert_equal(expected, result)

with pytest.raises(ValueError, match="['space'] not found"):
da.drop_duplicates("space", keep=keep)

def test_drop_duplicates_2d(self):
da = xr.DataArray(
[[0, 5, 6, 7], [2, 1, 3, 4]],
dims=["space", "time"],
coords={"space": [10, 10], "time": [0, 0, 1, 2]},
name="test",
)

expected = xr.DataArray(
[[0, 6, 7]],
dims=["space", "time"],
coords={"time": ("time", [0, 1, 2]), "space": ("space", [10])},
name="test",
)

result = da.drop_duplicates(["time", "space"], keep="first")
assert_equal(expected, result)

expected = xr.DataArray(data, dims="time", coords={"time": time}, name="test")
result = ds.drop_duplicates("time", keep=keep)
assert_equal(expected, result)
result = da.drop_duplicates(..., keep="first")
assert_equal(expected, result)


class TestNumpyCoercion:
Expand Down
31 changes: 31 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6546,6 +6546,37 @@ def test_clip(ds):
assert result.dims == ds.dims


class TestDropDuplicates:
@pytest.mark.parametrize("keep", ["first", "last", False])
def test_drop_duplicates_1d(self, keep):
ds = xr.Dataset(
{"a": ("time", [0, 5, 6, 7]), "b": ("time", [9, 3, 8, 2])},
coords={"time": [0, 0, 1, 2]},
)

if keep == "first":
a = [0, 6, 7]
b = [9, 8, 2]
time = [0, 1, 2]
elif keep == "last":
a = [5, 6, 7]
b = [3, 8, 2]
time = [0, 1, 2]
else:
a = [6, 7]
b = [8, 2]
time = [1, 2]

expected = xr.Dataset(
{"a": ("time", a), "b": ("time", b)}, coords={"time": time}
)
result = ds.drop_duplicates("time", keep=keep)
assert_equal(expected, result)

with pytest.raises(ValueError, match="['space'] not found"):
ds.drop_duplicates("space", keep=keep)


class TestNumpyCoercion:
def test_from_numpy(self):
ds = xr.Dataset({"a": ("x", [1, 2, 3])}, coords={"lat": ("x", [4, 5, 6])})
Expand Down