Skip to content

Commit dc5ef66

Browse files
BUG: SparseArray.unique with all sparse (#23186)
* BUG: SparseArray.unique with all sparse * fixup
1 parent 1546c35 commit dc5ef66

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ Sparse
986986
- Improved performance of :meth:`Series.shift` for non-NA ``fill_value``, as values are no longer converted to a dense array.
987987
- Bug in ``DataFrame.groupby`` not including ``fill_value`` in the groups for non-NA ``fill_value`` when grouping by a sparse column (:issue:`5078`)
988988
- Bug in unary inversion operator (``~``) on a ``SparseSeries`` with boolean values. The performance of this has also been improved (:issue:`22835`)
989+
- Bug in :meth:`SparseArary.unique` not returning the unique values (:issue:`19595`)
989990

990991
Build Changes
991992
^^^^^^^^^^^^^

pandas/core/arrays/sparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ def _first_fill_value_loc(self):
809809
return -1
810810

811811
indices = self.sp_index.to_int_index().indices
812-
if indices[0] > 0:
812+
if not len(indices) or indices[0] > 0:
813813
return 0
814814

815815
diff = indices[1:] - indices[:-1]

pandas/tests/arrays/sparse/test_array.py

+8
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,14 @@ def test_unique_na_fill(arr, fill_value):
10651065
tm.assert_numpy_array_equal(a, b)
10661066

10671067

1068+
def test_unique_all_sparse():
1069+
# https://github.com/pandas-dev/pandas/issues/23168
1070+
arr = SparseArray([0, 0])
1071+
result = arr.unique()
1072+
expected = SparseArray([0])
1073+
tm.assert_sp_array_equal(result, expected)
1074+
1075+
10681076
def test_map():
10691077
arr = SparseArray([0, 1, 2])
10701078
expected = SparseArray([10, 11, 12], fill_value=10)

0 commit comments

Comments
 (0)