def _isnull_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.isnull() 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 = lib.isnullobj(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 _isnull_ndarraylike(obj): from pandas import Series values = np.asarray(obj) if values.dtype.kind in ('O', 'S', 'U'): # Working around NumPy ticket 1542 shape = values.shape if values.dtype.kind in ('S', 'U'): result = np.zeros(values.shape, dtype=bool) else: result = np.empty(shape, dtype=bool) vec = lib.isnullobj(values.ravel()) result[:] = vec.reshape(shape) if isinstance(obj, Series): result = Series(result, index=obj.index, copy=False) elif values.dtype == np.dtype('M8[ns]'): # this is the NaT pattern result = values.view('i8') == lib.iNaT elif issubclass(values.dtype.type, np.timedelta64): result = -np.isfinite(values.view('i8')) else: result = -np.isfinite(obj) return result
def _isnull_ndarraylike(obj): from pandas import Series values = np.asarray(obj) if values.dtype.kind in ("O", "S"): # Working around NumPy ticket 1542 shape = values.shape result = np.empty(shape, dtype=bool) vec = lib.isnullobj(values.ravel()) result[:] = vec.reshape(shape) if isinstance(obj, Series): result = Series(result, index=obj.index, copy=False) elif values.dtype == np.dtype("M8[ns]"): # this is the NaT pattern result = values.view("i8") == lib.iNaT else: result = -np.isfinite(obj) return result
def isnull(obj): ''' Replacement for numpy.isnan / -numpy.isfinite which is suitable for use on object arrays. Parameters ---------- arr: ndarray or object value Returns ------- boolean ndarray or boolean ''' if lib.isscalar(obj): return lib.checknull(obj) from pandas.core.generic import PandasObject from pandas import Series if isinstance(obj, np.ndarray): if obj.dtype.kind in ('O', 'S'): # Working around NumPy ticket 1542 shape = obj.shape result = np.empty(shape, dtype=bool) vec = lib.isnullobj(obj.ravel()) result[:] = vec.reshape(shape) if isinstance(obj, Series): result = Series(result, index=obj.index, copy=False) elif obj.dtype == np.dtype('M8[ns]'): # this is the NaT pattern result = np.array(obj).view('i8') == lib.iNaT else: result = -np.isfinite(obj) return result elif isinstance(obj, PandasObject): # TODO: optimize for DataFrame, etc. return obj.apply(isnull) else: return obj is None