示例#1
0
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
示例#2
0
文件: missing.py 项目: Itay4/pandas
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
示例#3
0
文件: missing.py 项目: yaruyi/pandas
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 is_extension_array_dtype(dtype):
        if inf_as_na and is_categorical_dtype(dtype):
            result = libmissing.isnaobj_old(values.to_numpy())
        else:
            result = values.isna()
    elif is_string_dtype(dtype):
        result = _isna_string_dtype(values, dtype, 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
示例#4
0
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 is_extension_array_dtype(dtype):
        if inf_as_na and is_categorical_dtype(dtype):
            # error: Item "ndarray" of "Union[ExtensionArray, ndarray]" has no attribute
            # "to_numpy"
            result = libmissing.isnaobj_old(
                values.to_numpy()  # type: ignore[union-attr]
            )
        else:
            # error: Item "ndarray" of "Union[ExtensionArray, ndarray]" has no attribute
            # "isna"
            result = values.isna()  # type: ignore[union-attr]
    elif is_string_dtype(dtype):
        # error: Argument 1 to "_isna_string_dtype" has incompatible type
        # "ExtensionArray"; expected "ndarray"
        # error: Argument 2 to "_isna_string_dtype" has incompatible type
        # "ExtensionDtype"; expected "dtype[Any]"
        result = _isna_string_dtype(
            values,
            dtype,
            inf_as_na=inf_as_na  # type: ignore[arg-type]
        )
    elif needs_i8_conversion(dtype):
        # this is the NaT pattern
        result = values.view("i8") == iNaT
    else:
        if inf_as_na:
            # error: Argument 1 to "__call__" of "ufunc" has incompatible type
            # "ExtensionArray"; expected "Union[Union[int, float, complex, str, bytes,
            # generic], Sequence[Union[int, float, complex, str, bytes, generic]],
            # Sequence[Sequence[Any]], _SupportsArray]"
            result = ~np.isfinite(values)  # type: ignore[arg-type]
        else:
            # error: Argument 1 to "__call__" of "ufunc" has incompatible type
            # "ExtensionArray"; expected "Union[Union[int, float, complex, str, bytes,
            # generic], Sequence[Union[int, float, complex, str, bytes, generic]],
            # Sequence[Sequence[Any]], _SupportsArray]"
            result = np.isnan(values)  # type: ignore[arg-type]

    return result
示例#5
0
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
示例#6
0
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)
        if inf_as_na:
            vec = libmissing.isnaobj_old(values.ravel())
        else:
            vec = libmissing.isnaobj(values.ravel())

        result[...] = vec.reshape(shape)

    return result
示例#7
0
def _isna_ndarraylike(obj, inf_as_na: bool = False):
    """
    Return an array indicating which values of the input array are NaN / NA.

    Parameters
    ----------
    obj: array-like
        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.
    """
    values = getattr(obj, "_values", obj)
    dtype = values.dtype

    if is_extension_array_dtype(dtype):
        if inf_as_na and is_categorical_dtype(dtype):
            result = libmissing.isnaobj_old(values.to_numpy())
        else:
            result = values.isna()
    elif is_string_dtype(dtype):
        result = _isna_string_dtype(values, dtype, 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)

    # box
    if isinstance(obj, ABCSeries):
        result = obj._constructor(result,
                                  index=obj.index,
                                  name=obj.name,
                                  copy=False)

    return result