def _preprocess_host_value(self, value, dtype): value = to_cudf_compatible_scalar(value, dtype=dtype) valid = not _is_null_host_scalar(value) 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
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
def __getitem__(self, arg): if isinstance(arg, tuple): arg = list(arg) data = self._sr._column[arg] if is_scalar(data) or _is_null_host_scalar(data): return data index = self._sr.index.take(arg) return self._sr._copy_construct(data=data, index=index)
def __getitem__(self, arg): if isinstance(arg, tuple): arg = list(arg) data = self._sr._column[arg] if (isinstance(data, (dict, list)) or _is_scalar_or_zero_d_array(data) or _is_null_host_scalar(data)): return data return self._sr._from_data({self._sr.name: data}, index=cudf.Index(self._sr.index.take(arg)))
def to_cudf_compatible_scalar(val, dtype=None): """ Converts the value `val` to a numpy/Pandas scalar, optionally casting to `dtype`. If `val` is None, returns None. """ if _is_null_host_scalar(val) or isinstance(val, cudf.Scalar): return val if not is_scalar(val): raise ValueError( f"Cannot convert value of type {type(val).__name__} " " to cudf scalar" ) if isinstance(val, (np.ndarray, cp.ndarray)) and val.ndim == 0: val = val.item() if ((dtype is None) and isinstance(val, str)) or is_string_dtype(dtype): dtype = "str" if isinstance(val, dt.datetime): val = np.datetime64(val) elif isinstance(val, dt.timedelta): val = np.timedelta64(val) elif isinstance(val, pd.Timestamp): val = val.to_datetime64() elif isinstance(val, pd.Timedelta): val = val.to_timedelta64() val = pd.api.types.pandas_dtype(type(val)).type(val) if dtype is not None: val = val.astype(dtype) if val.dtype.type is np.datetime64: time_unit, _ = np.datetime_data(val.dtype) if time_unit in ("D", "W", "M", "Y"): val = val.astype("datetime64[s]") elif val.dtype.type is np.timedelta64: time_unit, _ = np.datetime_data(val.dtype) if time_unit in ("D", "W", "M", "Y"): val = val.astype("timedelta64[ns]") return val
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
def is_valid(self): if not self._is_host_value_current: self._device_value_to_host() return not _is_null_host_scalar(self._host_value)