Skip to content

Commit 13bbe7c

Browse files
author
MomIsBestFriend
committed
BUG: fix the return value of .all() and .any() on pd.Series
1 parent 28e909c commit 13bbe7c

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

pandas/core/nanops.py

+35-4
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,24 @@ def nanany(values, axis=None, skipna: bool = True, mask=None):
421421
>>> nanops.nanany(s)
422422
False
423423
"""
424-
values, _, _, _, _ = _get_values(values, skipna, fill_value=False, mask=mask)
425-
return values.any(axis)
424+
values, _, dtype, _, _ = _get_values(values, skipna, fill_value=False, mask=mask)
426425

426+
# GH #12863
427+
# Checking if the `axis` is None because numpy
428+
# doesn't handle ``any`` and ``all`` on
429+
# object arrays correctly. see
430+
# https://github.com/numpy/numpy/issues/4352
431+
432+
# TODO: Find a less code-smelly way of doing this
433+
if is_object_dtype(dtype) and axis is None:
434+
output = values.any()
435+
else:
436+
output = values.any(axis)
437+
438+
if isinstance(output, bool):
439+
return output
440+
441+
return any(values)
427442

428443
def nanall(values, axis=None, skipna: bool = True, mask=None):
429444
"""
@@ -453,8 +468,24 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
453468
>>> nanops.nanall(s)
454469
False
455470
"""
456-
values, _, _, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
457-
return values.all(axis)
471+
values, _, dtype, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
472+
473+
# GH #12863
474+
# Checking if the `axis` is None because numpy
475+
# doesn't handle ``any`` and ``all`` on
476+
# object arrays correctly. see
477+
# https://github.com/numpy/numpy/issues/4352
478+
479+
# TODO: Find a less code-smelly way of doing this
480+
if is_object_dtype(dtype) and axis is None:
481+
output = values.all()
482+
else:
483+
output = values.all(axis)
484+
485+
if isinstance(output, bool):
486+
return output
487+
488+
return all(values)
458489

459490

460491
@disallow("M8")

pandas/tests/reductions/test_reductions.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -811,19 +811,34 @@ def test_all_any(self):
811811
assert not bool_series.all()
812812
assert bool_series.any()
813813

814-
# Alternative types, with implicit 'object' dtype.
815-
s = Series(["abc", True])
816-
assert "abc" == s.any() # 'abc' || True => 'abc'
814+
# GH 12863
815+
s1 = Series(["abc", True])
816+
s2 = Series(["abc", False])
817+
818+
assert s1.all()
819+
assert s1.any()
820+
821+
assert not s2.all()
822+
assert s2.any()
817823

818824
def test_all_any_params(self):
819825
# Check skipna, with implicit 'object' dtype.
820826
s1 = Series([np.nan, True])
821827
s2 = Series([np.nan, False])
822-
assert s1.all(skipna=False) # nan && True => True
828+
829+
# GH #12863
823830
assert s1.all(skipna=True)
824-
assert np.isnan(s2.any(skipna=False)) # nan || False => nan
831+
assert s1.any(skipna=True)
832+
833+
assert s1.all(skipna=False)
834+
assert s1.any(skipna=False)
835+
836+
assert not s2.all(skipna=True)
825837
assert not s2.any(skipna=True)
826838

839+
assert not s2.all(skipna=False)
840+
assert s2.any(skipna=False)
841+
827842
# Check level.
828843
s = pd.Series([False, False, True, True, False, True], index=[0, 0, 1, 1, 2, 2])
829844
tm.assert_series_equal(s.all(level=0), Series([False, True, False]))

pandas/tests/series/test_apply.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ def test_non_callable_aggregates(self):
390390
("sum", "abc"),
391391
("max", "c"),
392392
("min", "a"),
393-
("all", "c"), # see GH12863
394-
("any", "a"),
393+
("all", True),
394+
("any", True),
395395
],
396396
),
397397
),

pandas/tests/test_nanops.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,17 @@ def _badobj_wrap(self, value, func, allow_complex=True, **kwargs):
270270
value = value.astype("f8")
271271
return func(value, **kwargs)
272272

273+
# GH #12863
274+
# Disabled until https://github.com/numpy/numpy/issues/4352
275+
# is fixed
276+
@pytest.mark.xfail(reason="disabled")
273277
@pytest.mark.parametrize(
274278
"nan_op,np_op", [(nanops.nanany, np.any), (nanops.nanall, np.all)]
275279
)
276280
def test_nan_funcs(self, nan_op, np_op):
277281
# TODO: allow tdelta, doesn't break tests
278282
self.check_funs(
279-
nan_op, np_op, allow_all_nan=False, allow_date=False, allow_tdelta=False
283+
nan_op, np_op, allow_all_nan=True, allow_date=False, allow_tdelta=False
280284
)
281285

282286
def test_nansum(self):

0 commit comments

Comments
 (0)