From dc5ef66a35cf17a605e549b9253fef5d976e46b4 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 18 Oct 2018 06:25:56 -0500 Subject: [PATCH] BUG: SparseArray.unique with all sparse (#23186) * BUG: SparseArray.unique with all sparse * fixup --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/arrays/sparse.py | 2 +- pandas/tests/arrays/sparse/test_array.py | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 16f0b9ee99909..894ac83c1a361 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -986,6 +986,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 f5e54e4425444..e6ca7b8de83e4 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 0257d996228df..c6f777863265c 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)