Skip to content

Commit 82aafd4

Browse files
author
MomIsBestFriend
committed
Added brute force functions
1 parent 2c99fc2 commit 82aafd4

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

pandas/core/nanops.py

+58-5
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,37 @@ def nanany(values, axis=None, skipna: bool = True, mask=None):
428428
# doesn't handle ``any`` and ``all`` on
429429
# object arrays correclty. see
430430
# https://github.com/numpy/numpy/issues/4352
431+
432+
# TODO: Find a less code-smelly way of doing this
431433
if is_object_dtype(dtype) and axis is None:
432-
return np.any(values)
434+
output = values.any()
435+
else:
436+
output = values.any(axis)
437+
438+
if isinstance(output, bool):
439+
return output
440+
441+
# Bruteforce way
442+
def is_any(values: np.ndarray) -> bool:
443+
"""
444+
Check if any elements along an axis evaluate to True.
445+
446+
Parameters
447+
----------
448+
values : np.ndarray
449+
Array to iterate over it's values.
450+
451+
Returns
452+
-------
453+
bool
454+
Indicating if any of the values in the array, evaluates to True.
455+
"""
456+
for value in values.tolist():
457+
if bool(value):
458+
return True
459+
return False
433460

434-
return values.any(axis)
461+
return is_any(values)
435462

436463

437464
def nanall(values, axis=None, skipna: bool = True, mask=None):
@@ -469,11 +496,37 @@ def nanall(values, axis=None, skipna: bool = True, mask=None):
469496
# doesn't handle ``any`` and ``all`` on
470497
# object arrays correclty. see
471498
# https://github.com/numpy/numpy/issues/4352
472-
if is_object_dtype(dtype) and axis is None:
473-
return np.all(values)
474499

475-
return values.all(axis)
500+
# TODO: Find a less code-smelly way of doing this
501+
if is_object_dtype(dtype) and axis is None:
502+
output = values.all()
503+
else:
504+
output = values.all(axis)
505+
506+
if isinstance(output, bool):
507+
return output
508+
509+
# Bruteforce way
510+
def is_all(values: np.ndarray) -> bool:
511+
"""
512+
Check if all elements along an axis evaluate to True.
513+
514+
Parameters
515+
----------
516+
values : np.ndarray
517+
Array to iterate over it's values.
518+
519+
Returns
520+
-------
521+
bool
522+
Indicating if all of the values in the array, evaluates to True.
523+
"""
524+
for value in values.tolist():
525+
if not bool(value):
526+
return False
527+
return True
476528

529+
return is_all(values)
477530

478531
@disallow("M8")
479532
def nansum(values, axis=None, skipna=True, min_count=0, mask=None):

0 commit comments

Comments
 (0)