Skip to content

Add unique method #5091

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -292,6 +292,7 @@ DataArray contents
DataArray.drop_vars
DataArray.reset_coords
DataArray.copy
DataArray.unique

**ndarray methods**:
:py:attr:`~DataArray.astype`
Expand Down
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ New Features
:py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas'
:py:meth:`~pandas.core.groupby.GroupBy.get_group`.
By `Deepak Cherian <https://github.com/dcherian>`_.
- Implement :py:meth:`DataArray.unique` to return a flattened NumPy array
of unique values in the data array; convenient for iteration (:pull:`5091`).
By `Andrew Huang <https://github.com/ahuang11>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
14 changes: 14 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4418,6 +4418,20 @@ def query(
)
return ds[self.name]

def unique(self):
"""Return a raveled NumPy array of all the unique values in the
data array; can be convenient for iteration. Uniques are returned
in order of appearance. This does NOT sort.

Returns
-------
Returns
-------
ndarray
The unique values returned as a NumPy array..
"""
return pd.unique(self.values.ravel())

# this needs to be at the end, or mypy will confuse with `str`
# https://mypy.readthedocs.io/en/latest/common_issues.html#dealing-with-conflicting-names
str = utils.UncachedAccessor(StringAccessor)
Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7259,3 +7259,15 @@ def test_deepcopy_obj_array():
x0 = DataArray(np.array([object()]))
x1 = deepcopy(x0)
assert x0.values[0] is not x1.values[0]


def test_unique_order():
expected = np.array([0, 2, 1, 3])
result = DataArray([0, 2, 1, 1, 3, 3, 3, 2]).unique()
assert (expected == result).all()


def test_unique_2d():
expected = np.array([0, 2, 1, 3])
result = xr.DataArray([[0, 2, 1, 1], [3, 3, 3, 2]]).unique()
assert (expected == result).all()