def _check_behavior(self, arr, expected): result = libmissing.isnaobj(arr) tm.assert_numpy_array_equal(result, expected) result = libmissing.isnaobj(arr, inf_as_na=True) tm.assert_numpy_array_equal(result, expected) arr = np.atleast_2d(arr) expected = np.atleast_2d(expected) result = libmissing.isnaobj2d(arr) tm.assert_numpy_array_equal(result, expected) result = libmissing.isnaobj2d(arr, inf_as_na=True) tm.assert_numpy_array_equal(result, expected)
def _isna_ndarraylike(obj): values = getattr(obj, 'values', obj) dtype = values.dtype if is_extension_array_dtype(obj): if isinstance(obj, (ABCIndexClass, ABCSeries)): values = obj._values else: values = obj result = values.isna() elif is_string_dtype(dtype): # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): # object array of strings result = np.zeros(values.shape, dtype=bool) else: # object array of non-strings result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) elif needs_i8_conversion(obj): # this is the NaT pattern result = values.view('i8') == iNaT else: result = np.isnan(values) # box if isinstance(obj, ABCSeries): from pandas import Series result = Series(result, index=obj.index, name=obj.name, copy=False) return result
def _isna_ndarraylike(obj): values = getattr(obj, 'values', obj) dtype = values.dtype if is_extension_array_dtype(obj): if isinstance(obj, (ABCIndexClass, ABCSeries)): values = obj._values else: values = obj result = values.isna() elif is_string_dtype(dtype): # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): # object array of strings result = np.zeros(values.shape, dtype=bool) else: # object array of non-strings result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) elif needs_i8_conversion(obj): # this is the NaT pattern result = values.view('i8') == iNaT else: result = np.isnan(values) # box if isinstance(obj, ABCSeries): from pandas import Series result = Series(result, index=obj.index, name=obj.name, copy=False) return result
def _isna_string_dtype(values: np.ndarray, inf_as_na: bool) -> npt.NDArray[np.bool_]: # Working around NumPy ticket 1542 dtype = values.dtype if dtype.kind in ("S", "U"): result = np.zeros(values.shape, dtype=bool) else: if values.ndim == 1: result = libmissing.isnaobj(values, inf_as_na=inf_as_na) elif values.ndim == 2: result = libmissing.isnaobj2d(values, inf_as_na=inf_as_na) else: # 0-D, reached via e.g. mask_missing result = libmissing.isnaobj(values.ravel(), inf_as_na=inf_as_na) result = result.reshape(values.shape) return result
def _isna_string_dtype(values: np.ndarray, inf_as_na: bool) -> np.ndarray: # Working around NumPy ticket 1542 dtype = values.dtype shape = values.shape if dtype.kind in ("S", "U"): result = np.zeros(values.shape, dtype=bool) else: result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel(), inf_as_na=inf_as_na) result[...] = vec.reshape(shape) return result
def _isna_string_dtype(values: np.ndarray, dtype: np.dtype, old: bool) -> np.ndarray: # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): result = np.zeros(values.shape, dtype=bool) else: result = np.empty(shape, dtype=bool) if old: vec = libmissing.isnaobj_old(values.ravel()) else: vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) return result
def _isna_ndarraylike(obj): is_extension = is_extension_array_dtype(obj) if not is_extension: # Avoid accessing `.values` on things like # PeriodIndex, which may be expensive. values = getattr(obj, "values", obj) else: values = obj dtype = values.dtype if is_extension: if isinstance(obj, (ABCIndexClass, ABCSeries)): values = obj._values else: values = obj result = values.isna() elif isinstance(obj, ABCDatetimeArray): return obj.isna() elif is_string_dtype(dtype): # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): # object array of strings result = np.zeros(values.shape, dtype=bool) else: # object array of non-strings result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) elif needs_i8_conversion(dtype): # this is the NaT pattern result = values.view("i8") == iNaT else: result = np.isnan(values) # box if isinstance(obj, ABCSeries): result = obj._constructor(result, index=obj.index, name=obj.name, copy=False) return result
def _isna_ndarraylike(obj): is_extension = is_extension_array_dtype(obj) if not is_extension: # Avoid accessing `.values` on things like # PeriodIndex, which may be expensive. values = getattr(obj, 'values', obj) else: values = obj dtype = values.dtype if is_extension: if isinstance(obj, (ABCIndexClass, ABCSeries)): values = obj._values else: values = obj result = values.isna() elif isinstance(obj, ABCDatetimeArray): return obj.isna() elif is_string_dtype(dtype): # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): # object array of strings result = np.zeros(values.shape, dtype=bool) else: # object array of non-strings result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) elif needs_i8_conversion(dtype): # this is the NaT pattern result = values.view('i8') == iNaT else: result = np.isnan(values) # box if isinstance(obj, ABCSeries): result = obj._constructor( result, index=obj.index, name=obj.name, copy=False) return result
def _isna_array(values: ArrayLike, inf_as_na: bool = False): """ Return an array indicating which values of the input array are NaN / NA. Parameters ---------- obj: ndarray or ExtensionArray The input array whose elements are to be checked. inf_as_na: bool Whether or not to treat infinite values as NA. Returns ------- array-like Array of boolean values denoting the NA status of each element. """ dtype = values.dtype if not isinstance(values, np.ndarray): # i.e. ExtensionArray if inf_as_na and is_categorical_dtype(dtype): result = libmissing.isnaobj(values.to_numpy(), inf_as_na=inf_as_na) else: # error: Incompatible types in assignment (expression has type # "Union[ndarray[Any, Any], ExtensionArraySupportsAnyAll]", variable has # type "ndarray[Any, dtype[bool_]]") result = values.isna() # type: ignore[assignment] elif is_string_or_object_np_dtype(values.dtype): result = _isna_string_dtype(values, inf_as_na=inf_as_na) elif needs_i8_conversion(dtype): # this is the NaT pattern result = values.view("i8") == iNaT else: if inf_as_na: result = ~np.isfinite(values) else: result = np.isnan(values) return result
def _isna_ndarraylike(obj): values = getattr(obj, 'values', obj) dtype = values.dtype if is_string_dtype(dtype): if is_categorical_dtype(values): from pandas import Categorical if not isinstance(values, Categorical): values = values.values result = values.isna() elif is_interval_dtype(values): from pandas import IntervalIndex result = IntervalIndex(obj).isna() else: # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): result = np.zeros(values.shape, dtype=bool) else: result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) elif needs_i8_conversion(obj): # this is the NaT pattern result = values.view('i8') == iNaT else: result = np.isnan(values) # box if isinstance(obj, ABCSeries): from pandas import Series result = Series(result, index=obj.index, name=obj.name, copy=False) return result
def _isna_ndarraylike(obj): values = getattr(obj, 'values', obj) dtype = values.dtype if is_string_dtype(dtype): if is_categorical_dtype(values): from pandas import Categorical if not isinstance(values, Categorical): values = values.values result = values.isna() elif is_interval_dtype(values): from pandas import IntervalIndex result = IntervalIndex(obj).isna() else: # Working around NumPy ticket 1542 shape = values.shape if is_string_like_dtype(dtype): result = np.zeros(values.shape, dtype=bool) else: result = np.empty(shape, dtype=bool) vec = libmissing.isnaobj(values.ravel()) result[...] = vec.reshape(shape) elif needs_i8_conversion(obj): # this is the NaT pattern result = values.view('i8') == iNaT else: result = np.isnan(values) # box if isinstance(obj, ABCSeries): from pandas import Series result = Series(result, index=obj.index, name=obj.name, copy=False) return result
def _isna_array(values: ArrayLike, inf_as_na: bool = False): """ Return an array indicating which values of the input array are NaN / NA. Parameters ---------- obj: ndarray or ExtensionArray The input array whose elements are to be checked. inf_as_na: bool Whether or not to treat infinite values as NA. Returns ------- array-like Array of boolean values denoting the NA status of each element. """ dtype = values.dtype if not isinstance(values, np.ndarray): # i.e. ExtensionArray if inf_as_na and is_categorical_dtype(dtype): result = libmissing.isnaobj(values.to_numpy(), inf_as_na=inf_as_na) else: result = values.isna() elif is_string_or_object_np_dtype(values.dtype): result = _isna_string_dtype(values, inf_as_na=inf_as_na) elif needs_i8_conversion(dtype): # this is the NaT pattern result = values.view("i8") == iNaT else: if inf_as_na: result = ~np.isfinite(values) else: result = np.isnan(values) return result