Skip to content

BUG: Sparse indexing with bool sparse may be incorrect #13985

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 1 commit 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/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ Note that the limitation is applied to ``fill_value`` which default is ``np.nan`
- Bug in ``SparseArray`` and ``SparseSeries`` don't apply ufunc to ``fill_value`` (:issue:`13853`)
- Bug in ``SparseSeries.abs`` incorrectly keeps negative ``fill_value`` (:issue:`13853`)
- Bug in single row slicing on multi-type ``SparseDataFrame``s, types were previously forced to float (:issue:`13917`)
- Bug in sparse indexing using ``SparseArray`` with ``bool`` dtype may return incorrect result (:issue:`13985`)

.. _whatsnew_0190.deprecations:

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
is_list_like,
is_sequence,
is_scalar,
is_sparse,
_ensure_platform_int)
from pandas.types.missing import isnull, _infer_fill_value

Expand Down Expand Up @@ -1811,9 +1812,10 @@ def check_bool_indexer(ax, key):
mask = isnull(result._values)
if mask.any():
raise IndexingError('Unalignable boolean Series key provided')

result = result.astype(bool)._values

elif is_sparse(result):
result = result.to_dense()
result = np.asarray(result, dtype=bool)
else:
# is_bool_indexer has already checked for nulls in the case of an
# object array key, so no check needed here
Expand Down
6 changes: 5 additions & 1 deletion pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pandas.types.generic import ABCSparseArray, ABCSparseSeries
from pandas.types.common import (is_float, is_integer,
is_integer_dtype, _ensure_platform_int,
is_bool_dtype,
is_list_like,
is_scalar, is_dtype_equal)
from pandas.types.cast import (_possibly_convert_platform, _maybe_promote,
Expand Down Expand Up @@ -385,7 +386,10 @@ def __getitem__(self, key):
data_slice = self.values[key]
else:
if isinstance(key, SparseArray):
key = np.asarray(key)
if is_bool_dtype(key):
key = key.to_dense()
else:
key = np.asarray(key)

if hasattr(key, '__len__') and len(self) != len(key):
return self.take(key)
Expand Down
1 change: 1 addition & 0 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ def take(self, indices, axis=0, convert=True, *args, **kwargs):
-------
taken : ndarray
"""

convert = nv.validate_take_with_convert(convert, args, kwargs)
new_values = SparseArray.take(self.values, indices)
new_index = self.index.take(indices)
Expand Down
61 changes: 61 additions & 0 deletions pandas/sparse/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def test_getitem(self):
exp = orig[orig % 2 == 1].to_sparse()
tm.assert_sp_series_equal(result, exp)

# sparse array
result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
tm.assert_sp_series_equal(result, exp)

def test_getitem_slice(self):
orig = self.orig
sparse = self.sparse
Expand Down Expand Up @@ -68,6 +72,10 @@ def test_getitem_fill_value(self):
exp = orig[orig % 2 == 1].to_sparse(fill_value=0)
tm.assert_sp_series_equal(result, exp)

# sparse array
result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
tm.assert_sp_series_equal(result, exp)

def test_getitem_ellipsis(self):
# GH 9467
s = pd.SparseSeries([1, np.nan, 2, 0, np.nan])
Expand Down Expand Up @@ -116,6 +124,10 @@ def test_loc(self):
exp = orig.loc[orig % 2 == 1].to_sparse()
tm.assert_sp_series_equal(result, exp)

# sparse array
result = sparse.loc[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
tm.assert_sp_series_equal(result, exp)

def test_loc_index(self):
orig = pd.Series([1, np.nan, np.nan, 3, np.nan], index=list('ABCDE'))
sparse = orig.to_sparse()
Expand All @@ -137,6 +149,10 @@ def test_loc_index(self):
exp = orig.loc[orig % 2 == 1].to_sparse()
tm.assert_sp_series_equal(result, exp)

# sparse array
result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
tm.assert_sp_series_equal(result, exp)

def test_loc_index_fill_value(self):
orig = pd.Series([1, np.nan, 0, 3, 0], index=list('ABCDE'))
sparse = orig.to_sparse(fill_value=0)
Expand Down Expand Up @@ -368,6 +384,35 @@ def test_reindex_fill_value(self):
exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse(fill_value=0)
tm.assert_sp_series_equal(res, exp)

def tests_indexing_with_sparse(self):
# GH 13985

for kind in ['integer', 'block']:
for fill in [True, False, np.nan]:
arr = pd.SparseArray([1, 2, 3], kind=kind)
indexer = pd.SparseArray([True, False, True], fill_value=fill,
dtype=bool)

tm.assert_sp_array_equal(pd.SparseArray([1, 3], kind=kind),
arr[indexer])

s = pd.SparseSeries(arr, index=['a', 'b', 'c'],
dtype=np.float64)
exp = pd.SparseSeries([1, 3], index=['a', 'c'],
dtype=np.float64, kind=kind)
tm.assert_sp_series_equal(s[indexer], exp)
tm.assert_sp_series_equal(s.loc[indexer], exp)
tm.assert_sp_series_equal(s.iloc[indexer], exp)

indexer = pd.SparseSeries(indexer, index=['a', 'b', 'c'])
tm.assert_sp_series_equal(s[indexer], exp)
tm.assert_sp_series_equal(s.loc[indexer], exp)

msg = ("iLocation based boolean indexing cannot use an "
"indexable as a mask")
with tm.assertRaisesRegexp(ValueError, msg):
s.iloc[indexer]


class TestSparseSeriesMultiIndexing(TestSparseSeriesIndexing):

Expand Down Expand Up @@ -405,6 +450,10 @@ def test_getitem_multi(self):
exp = orig[orig % 2 == 1].to_sparse()
tm.assert_sp_series_equal(result, exp)

# sparse array
result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
tm.assert_sp_series_equal(result, exp)

def test_getitem_multi_tuple(self):
orig = self.orig
sparse = self.sparse
Expand Down Expand Up @@ -454,6 +503,10 @@ def test_loc(self):
exp = orig.loc[orig % 2 == 1].to_sparse()
tm.assert_sp_series_equal(result, exp)

# sparse array
result = sparse.loc[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
tm.assert_sp_series_equal(result, exp)

def test_loc_multi_tuple(self):
orig = self.orig
sparse = self.sparse
Expand Down Expand Up @@ -578,6 +631,10 @@ def test_loc(self):
exp = orig.loc[orig.x % 2 == 1].to_sparse()
tm.assert_sp_frame_equal(result, exp)

# sparse array
result = sparse.loc[pd.SparseArray(sparse.x % 2 == 1, dtype=bool)]
tm.assert_sp_frame_equal(result, exp)

def test_loc_index(self):
orig = pd.DataFrame([[1, np.nan, np.nan],
[2, 3, np.nan],
Expand Down Expand Up @@ -627,6 +684,10 @@ def test_loc_index(self):
exp = orig.loc[orig.x % 2 == 1].to_sparse()
tm.assert_sp_frame_equal(result, exp)

# sparse array
result = sparse.loc[pd.SparseArray(sparse.x % 2 == 1, dtype=bool)]
tm.assert_sp_frame_equal(result, exp)

def test_loc_slice(self):
orig = pd.DataFrame([[1, np.nan, np.nan],
[2, 3, np.nan],
Expand Down