Skip to content

Commit 2217be0

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

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

pandas/core/nanops.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,27 @@ 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)
425+
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+
try:
442+
return any(values)
443+
except ValueError:
444+
return values.any()
426445

427446

428447
def nanall(values, axis=None, skipna: bool = True, mask=None):
@@ -453,8 +472,27 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
453472
>>> nanops.nanall(s)
454473
False
455474
"""
456-
values, _, _, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
457-
return values.all(axis)
475+
values, _, dtype, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
476+
477+
# GH #12863
478+
# Checking if the `axis` is None because numpy
479+
# doesn't handle ``any`` and ``all`` on
480+
# object arrays correctly. see
481+
# https://github.com/numpy/numpy/issues/4352
482+
483+
# TODO: Find a less code-smelly way of doing this
484+
if is_object_dtype(dtype) and axis is None:
485+
output = values.all()
486+
else:
487+
output = values.all(axis)
488+
489+
if isinstance(output, bool):
490+
return output
491+
492+
try:
493+
return all(values)
494+
except ValueError:
495+
return values.all()
458496

459497

460498
@disallow("M8")

pandas/tests/reductions/test_reductions.py

+23-6
Original file line numberDiff line numberDiff line change
@@ -811,19 +811,24 @@ 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'
817-
818814
def test_all_any_params(self):
819815
# Check skipna, with implicit 'object' dtype.
820816
s1 = Series([np.nan, True])
821817
s2 = Series([np.nan, False])
822-
assert s1.all(skipna=False) # nan && True => True
818+
819+
# GH #12863
823820
assert s1.all(skipna=True)
824-
assert np.isnan(s2.any(skipna=False)) # nan || False => nan
821+
assert s1.any(skipna=True)
822+
823+
assert s1.all(skipna=False)
824+
assert s1.any(skipna=False)
825+
826+
assert not s2.all(skipna=True)
825827
assert not s2.any(skipna=True)
826828

829+
assert not s2.all(skipna=False)
830+
assert s2.any(skipna=False)
831+
827832
# Check level.
828833
s = pd.Series([False, False, True, True, False, True], index=[0, 0, 1, 1, 2, 2])
829834
tm.assert_series_equal(s.all(level=0), Series([False, True, False]))
@@ -841,6 +846,18 @@ def test_all_any_params(self):
841846
with pytest.raises(NotImplementedError):
842847
s.all(bool_only=True)
843848

849+
def test_all_any_object_dtype(self):
850+
# GH 12863
851+
852+
s1 = Series(["abc", True])
853+
s2 = Series(["abc", False])
854+
855+
assert s1.all()
856+
assert s1.any()
857+
858+
assert not s2.all()
859+
assert s2.any()
860+
844861
def test_timedelta64_analytics(self):
845862

846863
# index min/max

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)