Exemplo n.º 1
0
    def normalize_binop_value(self, other: DatetimeLikeScalar) -> ScalarLike:
        if isinstance(other, cudf.Scalar):
            return other

        if isinstance(other, np.ndarray) and other.ndim == 0:
            other = other.item()

        if isinstance(other, dt.datetime):
            other = np.datetime64(other)
        elif isinstance(other, dt.timedelta):
            other = np.timedelta64(other)
        elif isinstance(other, pd.Timestamp):
            other = other.to_datetime64()
        elif isinstance(other, pd.Timedelta):
            other = other.to_timedelta64()
        elif isinstance(other, cudf.DateOffset):
            return other
        if isinstance(other, np.datetime64):
            if np.isnat(other):
                return cudf.Scalar(None, dtype=self.dtype)

            other = other.astype(self.dtype)
            return cudf.Scalar(other)
        elif isinstance(other, np.timedelta64):
            other_time_unit = cudf.utils.dtypes.get_time_unit(other)

            if other_time_unit not in ("s", "ms", "ns", "us"):
                other = other.astype("timedelta64[s]")

            if np.isnat(other):
                return cudf.Scalar(None, dtype=other.dtype)

            return cudf.Scalar(other)
        else:
            raise TypeError(f"cannot normalize {type(other)}")
Exemplo n.º 2
0
 def __contains__(self, item: DatetimeLikeScalar) -> bool:
     try:
         item = np.timedelta64(item, self._time_unit)
     except ValueError:
         # If item cannot be converted to duration type
         # np.timedelta64 raises ValueError, hence `item`
         # cannot exist in `self`.
         return False
     return item.view("int64") in self.as_numerical