Skip to content

Commit f9037cf

Browse files
author
MomIsBestFriend
committed
BUG: Fix for #12863
For 'any()' and 'all()' not returning bool when ndarray is an object
1 parent d0bb009 commit f9037cf

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

pandas/core/nanops.py

+12-4
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,11 @@ 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+
if is_object_dtype(dtype):
427+
return values.any()
428+
425429
return values.any(axis)
426430

427431

@@ -439,7 +443,7 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
439443
440444
Returns
441445
-------
442-
result : bool
446+
bool
443447
444448
Examples
445449
--------
@@ -453,7 +457,11 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
453457
>>> nanops.nanall(s)
454458
False
455459
"""
456-
values, _, _, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
460+
values, _, dtype, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
461+
462+
if is_object_dtype(dtype):
463+
return values.all()
464+
457465
return values.all(axis)
458466

459467

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]))

pandas/tests/series/test_apply.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ def test_non_callable_aggregates(self):
375375
("sum", "abc"),
376376
("max", "c"),
377377
("min", "a"),
378-
("all", "c"), # see GH12863
379-
("any", "a"),
378+
("all", True),
379+
("any", True),
380380
],
381381
),
382382
),

0 commit comments

Comments
 (0)