Skip to content

Commit 35391e9

Browse files
author
MomIsBestFriend
committed
Fix return value of 'any()' and 'all()' on pd.Series
1 parent a1405d5 commit 35391e9

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

pandas/core/nanops.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def nanany(values, axis=None, skipna: bool = True, mask=None):
407407
408408
Returns
409409
-------
410-
result : bool
410+
bool
411411
412412
Examples
413413
--------
@@ -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 values.size <= 2 and axis is None:
432+
return any(values)
433+
425434
return values.any(axis)
426435

427436

@@ -439,7 +448,7 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
439448
440449
Returns
441450
-------
442-
result : bool
451+
bool
443452
444453
Examples
445454
--------
@@ -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 values.size <= 2 and axis is None:
473+
return all(values)
474+
457475
return values.all(axis)
458476

459477

pandas/tests/reductions/test_reductions.py

Lines changed: 19 additions & 5 deletions
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)