Skip to content

BUG: Add SparseArray.all #17570

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

Merged
merged 6 commits into from
Sep 28, 2017
Merged
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.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ Sparse
- Bug in :func:`SparseDataFrame.fillna` not filling all NaNs when frame was instantiated from SciPy sparse matrix (:issue:`16112`)
- Bug in :func:`SparseSeries.unstack` and :func:`SparseDataFrame.stack` (:issue:`16614`, :issue:`15045`)
- Bug in :func:`make_sparse` treating two numeric/boolean data, which have same bits, as same when array ``dtype`` is ``object`` (:issue:`17574`)
- :func:`SparseArray.all` and :func:`SparseArray.any` are now implemented to handle ``SparseArray``, these were used but not implemented (:issue:`17570`)

Reshaping
^^^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ def validate_cum_func_with_skipna(skipna, args, kwargs, name):
return skipna


ALLANY_DEFAULTS = OrderedDict()
ALLANY_DEFAULTS['dtype'] = None
ALLANY_DEFAULTS['out'] = None
validate_all = CompatValidator(ALLANY_DEFAULTS, fname='all',
method='both', max_fname_arg_count=1)
validate_any = CompatValidator(ALLANY_DEFAULTS, fname='any',
method='both', max_fname_arg_count=1)

LOGICAL_FUNC_DEFAULTS = dict(out=None)
validate_logical_func = CompatValidator(LOGICAL_FUNC_DEFAULTS, method='kwargs')

Expand Down
42 changes: 42 additions & 0 deletions pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,48 @@ def fillna(self, value, downcast=None):
return self._simple_new(new_values, self.sp_index,
fill_value=fill_value)

def all(self, axis=0, *args, **kwargs):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well add any as well.

Tests whether all elements evaluate True

Returns
-------
all : bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a See Also to point to np.all


See Also
--------
numpy.all
"""
nv.validate_all(args, kwargs)

values = self.sp_values

if len(values) != len(self) and not np.all(self.fill_value):
return False

return values.all()

def any(self, axis=0, *args, **kwargs):
"""
Tests whether at least one of elements evaluate True

Returns
-------
any : bool

See Also
--------
numpy.any
"""
nv.validate_any(args, kwargs)

values = self.sp_values

if len(values) != len(self) and np.any(self.fill_value):
return True

return values.any()

def sum(self, axis=0, *args, **kwargs):
"""
Sum of non-NA/null values
Expand Down
88 changes: 88 additions & 0 deletions pandas/tests/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,94 @@ def test_fillna_overlap(self):

class TestSparseArrayAnalytics(object):

@pytest.mark.parametrize('data,pos,neg', [
([True, True, True], True, False),
([1, 2, 1], 1, 0),
([1.0, 2.0, 1.0], 1.0, 0.0)
])
def test_all(self, data, pos, neg):
# GH 17570
out = SparseArray(data).all()
assert out

out = SparseArray(data, fill_value=pos).all()
assert out

data[1] = neg
out = SparseArray(data).all()
assert not out

out = SparseArray(data, fill_value=pos).all()
assert not out

@pytest.mark.parametrize('data,pos,neg', [
([True, True, True], True, False),
([1, 2, 1], 1, 0),
([1.0, 2.0, 1.0], 1.0, 0.0)
])
def test_numpy_all(self, data, pos, neg):
# GH 17570
out = np.all(SparseArray(data))
assert out

out = np.all(SparseArray(data, fill_value=pos))
assert out

data[1] = neg
out = np.all(SparseArray(data))
assert not out

out = np.all(SparseArray(data, fill_value=pos))
assert not out

msg = "the 'out' parameter is not supported"
tm.assert_raises_regex(ValueError, msg, np.all,
SparseArray(data), out=out)

@pytest.mark.parametrize('data,pos,neg', [
([False, True, False], True, False),
([0, 2, 0], 2, 0),
([0.0, 2.0, 0.0], 2.0, 0.0)
])
def test_any(self, data, pos, neg):
# GH 17570
out = SparseArray(data).any()
assert out

out = SparseArray(data, fill_value=pos).any()
assert out

data[1] = neg
out = SparseArray(data).any()
assert not out

out = SparseArray(data, fill_value=pos).any()
assert not out

@pytest.mark.parametrize('data,pos,neg', [
([False, True, False], True, False),
([0, 2, 0], 2, 0),
([0.0, 2.0, 0.0], 2.0, 0.0)
])
def test_numpy_any(self, data, pos, neg):
# GH 17570
out = np.any(SparseArray(data))
assert out

out = np.any(SparseArray(data, fill_value=pos))
assert out

data[1] = neg
out = np.any(SparseArray(data))
assert not out

out = np.any(SparseArray(data, fill_value=pos))
assert not out

msg = "the 'out' parameter is not supported"
tm.assert_raises_regex(ValueError, msg, np.any,
SparseArray(data), out=out)

def test_sum(self):
data = np.arange(10).astype(float)
out = SparseArray(data).sum()
Expand Down