Skip to content

Commit 00c9b50

Browse files
author
MomIsBestFriend
committed
Fix return value of 'any()' and 'all()' on pd.Series
1 parent e81faa1 commit 00c9b50

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

pandas/core/nanops.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,16 @@ 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)
424+
values, _, dtype, _, _ = _get_values(values, skipna, fill_value=False, mask=mask)
425+
426+
# GH #12863
427+
# Checking if the `axis` is None because numpy
428+
# doesn't handle ``any`` and ``all`` on
429+
# object arrays correclty. see
430+
# https://github.com/numpy/numpy/issues/4352
431+
if is_object_dtype(dtype) and axis is None:
432+
return np.any(values)
433+
425434
return values.any(axis)
426435

427436

@@ -453,7 +462,16 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
453462
>>> nanops.nanall(s)
454463
False
455464
"""
456-
values, _, _, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
465+
values, _, dtype, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
466+
467+
# GH #12863
468+
# Checking if the `axis` is None because numpy
469+
# doesn't handle ``any`` and ``all`` on
470+
# object arrays correclty. see
471+
# https://github.com/numpy/numpy/issues/4352
472+
if is_object_dtype(dtype) and axis is None:
473+
return np.all(values)
474+
457475
return values.all(axis)
458476

459477

pandas/tests/reductions/test_reductions.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -811,19 +811,33 @@ 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+
823829
assert s1.all(skipna=True)
824-
assert np.isnan(s2.any(skipna=False)) # nan || False => nan
830+
assert s1.any(skipna=True)
831+
832+
assert s1.all(skipna=False)
833+
assert s1.any(skipna=False)
834+
835+
assert not s2.all(skipna=True)
825836
assert not s2.any(skipna=True)
826837

838+
assert not s2.all(skipna=False)
839+
assert s2.any(skipna=False)
840+
827841
# Check level.
828842
s = pd.Series([False, False, True, True, False, True], index=[0, 0, 1, 1, 2, 2])
829843
tm.assert_series_equal(s.all(level=0), Series([False, True, False]))

0 commit comments

Comments
 (0)