diff --git a/doc/api.rst b/doc/api.rst index 4af9bb01208..40913e2b721 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -292,6 +292,7 @@ DataArray contents DataArray.drop_vars DataArray.reset_coords DataArray.copy + DataArray.unique **ndarray methods**: :py:attr:`~DataArray.astype` diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 77d6296acac..8be5c6b7d2f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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 `_. +- 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 `_. Breaking changes ~~~~~~~~~~~~~~~~ diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 8c5df5575ac..65f5c9689a5 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -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) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 20e357543d6..8ca7a2cdc91 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -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()