def _isna_ndarraylike_old(obj): values = getattr(obj, "values", obj) dtype = values.dtype if is_string_dtype(dtype): # 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_old(values.ravel()) result[:] = vec.reshape(shape) elif is_datetime64_dtype(dtype): # this is the NaT pattern result = values.view("i8") == iNaT else: result = ~np.isfinite(values) # box if isinstance(obj, ABCSeries): result = obj._constructor(result, index=obj.index, name=obj.name, copy=False) return result
def _get_dtype_cmp_class(dtype): if is_numeric_dtype(dtype) or is_bool_dtype(dtype): return "numeric" if is_string_like_dtype(dtype) or is_categorical_dtype(dtype): return "string" if is_datetime64_any_dtype(dtype): return "datetime" return "other"
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 test_is_string_like_dtype(): assert not com.is_string_like_dtype(object) assert not com.is_string_like_dtype(pd.Series([1, 2])) assert com.is_string_like_dtype(str) assert com.is_string_like_dtype(np.array(['a', 'b']))