diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index c3e66b37a9da4c..c74f0bd9bcc34e 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -987,6 +987,7 @@ Sparse - Improved performance of :meth:`Series.shift` for non-NA ``fill_value``, as values are no longer converted to a dense array. - Bug in ``DataFrame.groupby`` not including ``fill_value`` in the groups for non-NA ``fill_value`` when grouping by a sparse column (:issue:`5078`) - Bug in unary inversion operator (``~``) on a ``SparseSeries`` with boolean values. The performance of this has also been improved (:issue:`22835`) +- Bug in :meth:`SparseArary.unique` not returning the unique values (:issue:`19595`) Build Changes ^^^^^^^^^^^^^ diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index f5e54e44254447..e6ca7b8de83e44 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -809,7 +809,7 @@ def _first_fill_value_loc(self): return -1 indices = self.sp_index.to_int_index().indices - if indices[0] > 0: + if not len(indices) or indices[0] > 0: return 0 diff = indices[1:] - indices[:-1] diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 0257d996228df6..c6f777863265c4 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -1065,6 +1065,14 @@ def test_unique_na_fill(arr, fill_value): tm.assert_numpy_array_equal(a, b) +def test_unique_all_sparse(): + # https://github.com/pandas-dev/pandas/issues/23168 + arr = SparseArray([0, 0]) + result = arr.unique() + expected = SparseArray([0]) + tm.assert_sp_array_equal(result, expected) + + def test_map(): arr = SparseArray([0, 1, 2]) expected = SparseArray([10, 11, 12], fill_value=10)