Ejemplo n.º 1
0
 def _maybe_cast_for_get_loc(self, key) -> Timestamp:
     # needed to localize naive datetimes
     key = Timestamp(key)
     if key.tzinfo is None:
         key = key.tz_localize(self.tz)
     else:
         key = key.tz_convert(self.tz)
     return key
Ejemplo n.º 2
0
    def get_loc(self, key, method=None, tolerance=None):
        """
        Get integer location for requested label

        Returns
        -------
        loc : int
        """

        if tolerance is not None:
            # try converting tolerance now, so errors don't get swallowed by
            # the try/except clauses below
            tolerance = self._convert_tolerance(tolerance, np.asarray(key))

        if isinstance(key, datetime):
            # needed to localize naive datetimes
            if key.tzinfo is None:
                key = Timestamp(key, tz=self.tz)
            else:
                key = Timestamp(key).tz_convert(self.tz)
            return Index.get_loc(self, key, method, tolerance)

        elif isinstance(key, timedelta):
            # GH#20464
            raise TypeError(
                f"Cannot index {type(self).__name__} with {type(key).__name__}"
            )

        if isinstance(key, time):
            if method is not None:
                raise NotImplementedError(
                    "cannot yet lookup inexact labels when key is a time object"
                )
            return self.indexer_at_time(key)

        try:
            return Index.get_loc(self, key, method, tolerance)
        except (KeyError, ValueError, TypeError):
            try:
                return self._get_string_slice(key)
            except (TypeError, KeyError, ValueError, OverflowError):
                pass

            try:
                stamp = Timestamp(key)
                if stamp.tzinfo is not None and self.tz is not None:
                    stamp = stamp.tz_convert(self.tz)
                else:
                    stamp = stamp.tz_localize(self.tz)
                return Index.get_loc(self, stamp, method, tolerance)
            except KeyError:
                raise KeyError(key)
            except ValueError as e:
                # list-like tolerance size must match target index size
                if "list-like" in str(e):
                    raise e
                raise KeyError(key)
Ejemplo n.º 3
0
 def get_value_maybe_box(self, series, key):
     # needed to localize naive datetimes
     if self.tz is not None:
         key = Timestamp(key)
         if key.tzinfo is not None:
             key = key.tz_convert(self.tz)
         else:
             key = key.tz_localize(self.tz)
     elif not isinstance(key, Timestamp):
         key = Timestamp(key)
     values = self._engine.get_value(com.values_from_object(series), key, tz=self.tz)
     return com.maybe_box(self, values, series, key)
Ejemplo n.º 4
0
    def _maybe_cast_for_get_loc(self, key) -> Timestamp:
        # needed to localize naive datetimes or dates (GH 35690)
        try:
            key = Timestamp(key)
        except ValueError as err:
            # FIXME(dateutil#1180): we get here because parse_with_reso
            #  doesn't raise on "t2m"
            if not isinstance(key, str):
                # Not expected to be reached, but check to be sure
                raise  # pragma: no cover
            raise KeyError(key) from err

        if key.tzinfo is None:
            key = key.tz_localize(self.tz)
        else:
            key = key.tz_convert(self.tz)
        return key
Ejemplo n.º 5
0
def _hash_scalar(val,
                 encoding: str = "utf8",
                 hash_key: str = _default_hash_key) -> np.ndarray:
    """
    Hash scalar value.

    Parameters
    ----------
    val : scalar
    encoding : str, default "utf8"
    hash_key : str, default _default_hash_key

    Returns
    -------
    1d uint64 numpy array of hash value, of length 1
    """

    if isna(val):
        # this is to be consistent with the _hash_categorical implementation
        return np.array([np.iinfo(np.uint64).max], dtype="u8")

    if getattr(val, "tzinfo", None) is not None:
        # for tz-aware datetimes, we need the underlying naive UTC value and
        # not the tz aware object or pd extension type (as
        # infer_dtype_from_scalar would do)
        if not isinstance(val, Timestamp):
            val = Timestamp(val)
        val = val.tz_convert(None)

    dtype, val = infer_dtype_from_scalar(val)
    vals = np.array([val], dtype=dtype)

    return hash_array(vals,
                      hash_key=hash_key,
                      encoding=encoding,
                      categorize=False)