Пример #1
0
    def _preprocess_host_value(self, value, dtype):
        valid = not _is_null_host_scalar(value)

        if isinstance(dtype, Decimal64Dtype):
            value = pa.scalar(value,
                              type=pa.decimal128(dtype.precision,
                                                 dtype.scale)).as_py()
        if isinstance(value, decimal.Decimal) and dtype is None:
            dtype = Decimal64Dtype._from_decimal(value)

        value = to_cudf_compatible_scalar(value, dtype=dtype)

        if dtype is None:
            if not valid:
                if isinstance(value, (np.datetime64, np.timedelta64)):
                    unit, _ = np.datetime_data(value)
                    if unit == "generic":
                        raise TypeError(
                            "Cant convert generic NaT to null scalar")
                    else:
                        dtype = value.dtype
                else:
                    raise TypeError(
                        "dtype required when constructing a null scalar")
            else:
                dtype = value.dtype

        if not isinstance(dtype, Decimal64Dtype):
            dtype = np.dtype(dtype)

        if not valid:
            value = NA

        return value, dtype
Пример #2
0
    def _preprocess_host_value(self, value, dtype):
        valid = not cudf._lib.scalar._is_null_host_scalar(value)

        if isinstance(value, list):
            if dtype is not None:
                raise TypeError("Lists may not be cast to a different dtype")
            else:
                dtype = ListDtype.from_arrow(
                    pa.infer_type([value], from_pandas=True))
                return value, dtype
        elif isinstance(dtype, ListDtype):
            if value not in {None, NA}:
                raise ValueError(f"Can not coerce {value} to ListDtype")
            else:
                return NA, dtype

        if isinstance(value, dict):
            if dtype is None:
                dtype = StructDtype.from_arrow(
                    pa.infer_type([value], from_pandas=True))
            return value, dtype
        elif isinstance(dtype, StructDtype):
            if value not in {None, NA}:
                raise ValueError(f"Can not coerce {value} to StructDType")
            else:
                return NA, dtype

        if isinstance(dtype, Decimal64Dtype):
            value = pa.scalar(value,
                              type=pa.decimal128(dtype.precision,
                                                 dtype.scale)).as_py()
        if isinstance(value, decimal.Decimal) and dtype is None:
            dtype = Decimal64Dtype._from_decimal(value)

        value = to_cudf_compatible_scalar(value, dtype=dtype)

        if dtype is None:
            if not valid:
                if isinstance(value, (np.datetime64, np.timedelta64)):
                    unit, _ = np.datetime_data(value)
                    if unit == "generic":
                        raise TypeError(
                            "Cant convert generic NaT to null scalar")
                    else:
                        dtype = value.dtype
                else:
                    raise TypeError(
                        "dtype required when constructing a null scalar")
            else:
                dtype = value.dtype

        if not isinstance(dtype, Decimal64Dtype):
            dtype = cudf.dtype(dtype)

        if not valid:
            value = NA

        return value, dtype
Пример #3
0
    def _preprocess_host_value(self, value, dtype):
        if isinstance(dtype, Decimal64Dtype):
            # TODO: Support coercion from decimal.Decimal to different dtype
            # TODO: Support coercion from integer to Decimal64Dtype
            raise NotImplementedError(
                "dtype as cudf.Decimal64Dtype is not supported. Pass a "
                "decimal.Decimal to construct a DecimalScalar.")
        if isinstance(value, decimal.Decimal) and dtype is not None:
            raise TypeError(f"Can not coerce decimal to {dtype}")

        value = to_cudf_compatible_scalar(value, dtype=dtype)
        valid = not _is_null_host_scalar(value)

        if isinstance(value, decimal.Decimal):
            # 0.0042 -> Decimal64Dtype(2, 4)
            dtype = Decimal64Dtype._from_decimal(value)

        else:
            if dtype is None:
                if not valid:
                    if isinstance(value, (np.datetime64, np.timedelta64)):
                        unit, _ = np.datetime_data(value)
                        if unit == "generic":
                            raise TypeError(
                                "Cant convert generic NaT to null scalar")
                        else:
                            dtype = value.dtype
                    else:
                        raise TypeError(
                            "dtype required when constructing a null scalar")
                else:
                    dtype = value.dtype
            dtype = np.dtype(dtype)

            # temporary
            dtype = np.dtype("object") if dtype.char == "U" else dtype

        if not valid:
            value = NA

        return value, dtype