diff --git a/python/cudf/cudf/__init__.py b/python/cudf/cudf/__init__.py index 273ab147241..27aaa07f940 100644 --- a/python/cudf/cudf/__init__.py +++ b/python/cudf/cudf/__init__.py @@ -17,10 +17,8 @@ register_index_accessor, register_series_accessor, ) -from cudf.core.scalar import ( - NA, - Scalar, -) +from cudf.core.scalar import Scalar + from cudf.core.index import ( BaseIndex, CategoricalIndex, @@ -45,6 +43,7 @@ ) from cudf.core.dataframe import DataFrame, from_pandas, merge, from_dataframe from cudf.core.series import Series +from cudf.core.missing import NA from cudf.core.multiindex import MultiIndex from cudf.core.cut import cut from cudf.core.algorithms import factorize diff --git a/python/cudf/cudf/core/missing.py b/python/cudf/cudf/core/missing.py new file mode 100644 index 00000000000..02bcb7636f4 --- /dev/null +++ b/python/cudf/cudf/core/missing.py @@ -0,0 +1,9 @@ +# Copyright (c) 2018-2022, NVIDIA CORPORATION. + + +# Pandas NAType enforces a single instance exists at a time +# instantiating this class will yield the existing instance +# of pandas._libs.missing.NAType, id(cudf.NA) == id(pd.NA). +from pandas import NA + +__all__ = ["NA"] diff --git a/python/cudf/cudf/core/scalar.py b/python/cudf/cudf/core/scalar.py index 1c81803ed98..189418f651f 100644 --- a/python/cudf/cudf/core/scalar.py +++ b/python/cudf/cudf/core/scalar.py @@ -4,14 +4,12 @@ import numpy as np import pyarrow as pa -from pandas._libs.missing import NAType as pd_NAType import cudf -from cudf.core.column.column import ColumnBase +from cudf.api.types import is_scalar from cudf.core.dtypes import ListDtype, StructDtype -from cudf.core.index import BaseIndex +from cudf.core.missing import NA from cudf.core.mixins import BinaryOperand -from cudf.core.series import Series from cudf.utils.dtypes import ( get_allowed_combinations_for_operator, to_cudf_compatible_scalar, @@ -273,19 +271,19 @@ def _binop_result_dtype_or_error(self, other, op): return cudf.dtype(out_dtype) def _binaryop(self, other, op: str): - if isinstance(other, (ColumnBase, Series, BaseIndex, np.ndarray)): - # dispatch to column implementation - return NotImplemented - other = to_cudf_compatible_scalar(other) - out_dtype = self._binop_result_dtype_or_error(other, op) - valid = self.is_valid and ( - isinstance(other, np.generic) or other.is_valid - ) - if not valid: - return Scalar(None, dtype=out_dtype) + if is_scalar(other): + other = to_cudf_compatible_scalar(other) + out_dtype = self._binop_result_dtype_or_error(other, op) + valid = self.is_valid and ( + isinstance(other, np.generic) or other.is_valid + ) + if not valid: + return Scalar(None, dtype=out_dtype) + else: + result = self._dispatch_scalar_binop(other, op) + return Scalar(result, dtype=out_dtype) else: - result = self._dispatch_scalar_binop(other, op) - return Scalar(result, dtype=out_dtype) + return NotImplemented def _dispatch_scalar_binop(self, other, op): if isinstance(other, Scalar): @@ -323,13 +321,3 @@ def _dispatch_scalar_unaop(self, op): def astype(self, dtype): return Scalar(self.value, dtype) - - -class _NAType(pd_NAType): - # Pandas NAType enforces a single instance exists at a time - # instantiating this class will yield the existing instance - # of pandas._libs.missing.NAType, id(cudf.NA) == id(pd.NA). - pass - - -NA = _NAType() diff --git a/python/cudf/cudf/core/udf/typing.py b/python/cudf/cudf/core/udf/typing.py index ed5fc1d6d23..37e2712f71c 100644 --- a/python/cudf/cudf/core/udf/typing.py +++ b/python/cudf/cudf/core/udf/typing.py @@ -17,8 +17,8 @@ ) from numba.core.typing.typeof import typeof from numba.cuda.cudadecl import registry as cuda_decl_registry -from pandas._libs.missing import NAType as _NAType +from cudf.core.missing import NA from cudf.core.udf import api from cudf.core.udf._ops import ( arith_ops, @@ -214,7 +214,7 @@ def unify(self, context, other): na_type = NAType() -@typeof_impl.register(_NAType) +@typeof_impl.register(type(NA)) def typeof_na(val, c): """ Tie instances of _NAType (cudf.NA) to our NAType. diff --git a/python/cudf/cudf/tests/test_udf_masked_ops.py b/python/cudf/cudf/tests/test_udf_masked_ops.py index 438f46d4266..73bf5160347 100644 --- a/python/cudf/cudf/tests/test_udf_masked_ops.py +++ b/python/cudf/cudf/tests/test_udf_masked_ops.py @@ -7,7 +7,7 @@ from numba import cuda import cudf -from cudf.core.scalar import NA +from cudf.core.missing import NA from cudf.core.udf._ops import ( arith_ops, bitwise_ops,