Compute a mask if and only if necessary. This function will compute a mask iff it is necessary. Otherwise, return the provided mask (potentially None) when a mask does not need to be computed. A mask is never necessary if the values array is of boolean or integer dtypes, a
(
values: np.ndarray, skipna: bool, mask: npt.NDArray[np.bool_] | None
)
| 210 | |
| 211 | |
| 212 | def _maybe_get_mask( |
| 213 | values: np.ndarray, skipna: bool, mask: npt.NDArray[np.bool_] | None |
| 214 | ) -> npt.NDArray[np.bool_] | None: |
| 215 | """ |
| 216 | Compute a mask if and only if necessary. |
| 217 | |
| 218 | This function will compute a mask iff it is necessary. Otherwise, |
| 219 | return the provided mask (potentially None) when a mask does not need to be |
| 220 | computed. |
| 221 | |
| 222 | A mask is never necessary if the values array is of boolean or integer |
| 223 | dtypes, as these are incapable of storing NaNs. If passing a NaN-capable |
| 224 | dtype that is interpretable as either boolean or integer data (eg, |
| 225 | timedelta64), a mask must be provided. |
| 226 | |
| 227 | If the skipna parameter is False, a new mask will not be computed. |
| 228 | |
| 229 | The mask is computed using isna() by default. Setting invert=True selects |
| 230 | notna() as the masking function. |
| 231 | |
| 232 | Parameters |
| 233 | ---------- |
| 234 | values : ndarray |
| 235 | input array to potentially compute mask for |
| 236 | skipna : bool |
| 237 | boolean for whether NaNs should be skipped |
| 238 | mask : Optional[ndarray] |
| 239 | nan-mask if known |
| 240 | |
| 241 | Returns |
| 242 | ------- |
| 243 | Optional[np.ndarray[bool]] |
| 244 | """ |
| 245 | if mask is None: |
| 246 | if values.dtype.kind in "biu": |
| 247 | # Boolean data cannot contain nulls, so signal via mask being None |
| 248 | return None |
| 249 | |
| 250 | if skipna or values.dtype.kind in "mM": |
| 251 | mask = isna(values) |
| 252 | |
| 253 | return mask |
| 254 | |
| 255 | |
| 256 | def _get_values( |