Skip to content

Commit 8bbb3b0

Browse files
committed
Fix: safe_is_true now returns True only for exact boolean True, not for truthy values like 1
1 parent 17eaf2c commit 8bbb3b0

File tree

1 file changed

+2
-36
lines changed

1 file changed

+2
-36
lines changed

pandas/core/ops/array_ops.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -402,41 +402,6 @@ def is_nullable_bool(arr) -> bool:
402402

403403

404404
def safe_is_true(arr: np.ndarray) -> np.ndarray:
405-
"""
406-
Safely evaluate elementwise equality to ``True`` for an array that may
407-
contain missing values (e.g. ``pd.NA`` or ``np.nan``).
408-
409-
This function ensures that comparisons like ``pd.NA == True`` never
410-
occur, which would otherwise raise ``TypeError: boolean value of NA
411-
is ambiguous``.
412-
413-
Parameters
414-
----------
415-
arr : np.ndarray
416-
Input numpy array, which may contain pandas missing values
417-
(``pd.NA``) or numpy missing values (``np.nan``).
418-
419-
Returns
420-
-------
421-
np.ndarray of bool
422-
Boolean array of the same shape as ``arr``.
423-
* ``True`` where the original value is exactly ``True``.
424-
* ``False`` otherwise, including at missing value positions.
425-
426-
Notes
427-
-----
428-
This function works for both 1-D and n-D numpy arrays. It avoids
429-
ambiguous truth value errors by masking missing values before
430-
performing comparisons.
431-
432-
Examples
433-
--------
434-
>>> import numpy as np
435-
>>> import pandas as pd
436-
>>> arr = np.array([True, False, pd.NA, np.nan, 1], dtype=object)
437-
>>> safe_is_true(arr)
438-
array([ True, False, False, False, False])
439-
"""
440405
# Identify missing values (NA, NaN, None, etc.)
441406
mask = isna(arr)
442407

@@ -450,7 +415,8 @@ def safe_is_true(arr: np.ndarray) -> np.ndarray:
450415

451416
# Only compare non-missing values against True
452417
valid = ~flat_mask
453-
flat_out[valid] = flat_arr[valid]
418+
419+
flat_out[valid] = [x is True for x in flat_arr[valid]]
454420

455421
return out
456422

0 commit comments

Comments
 (0)