New in version 1.0.0.
pandas allows indexing with NA values in a boolean array, which are treated as False.
Changed in version 1.0.2.
In : s = pd.Series([1, 2, 3])
In : mask = pd.array([True, False, pd.NA], dtype="boolean")
In : s[mask]
If you would prefer to keep the NA values you can manually fill them with fillna(True).
In : s[mask.fillna(True)]
arrays.BooleanArray implements Kleene Logic (sometimes called three-value logic) for
logical operations like & (and), | (or) and ^ (exclusive-or).
This table demonstrates the results for every combination. These operations are symmetrical,
so flipping the left- and right-hand side makes no difference in the result.
True & True
True & False
True & NA
False & False
False & NA
NA & NA
True | True
True | False
True | NA
False | False
False | NA
NA | NA
True ^ True
True ^ False
True ^ NA
False ^ False
False ^ NA
NA ^ NA
When an NA is present in an operation, the output value is NA only if
the result cannot be determined solely based on the other input. For example,
True | NA is True, because both True | True and True | False
are True. In that case, we don’t actually need to consider the value
of the NA.
On the other hand, True & NA is NA. The result depends on whether
the NA really is True or False, since True & True is True,
but True & False is False, so we can’t determine the output.
This differs from how np.nan behaves in logical operations. Pandas treated
np.nan is always false in the output.
In : pd.Series([True, False, np.nan], dtype="object") | True
In : pd.Series([True, False, np.nan], dtype="boolean") | True
In : pd.Series([True, False, np.nan], dtype="object") & True
In : pd.Series([True, False, np.nan], dtype="boolean") & True