From 0c68de36af3ddc6439ee311bbc805b3888ca34a2 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 30 Mar 2021 09:13:46 -0700 Subject: [PATCH] TYP: tslibs --- pandas/_libs/tslibs/conversion.pyi | 41 ++++++++++++++++++ pandas/_libs/tslibs/dtypes.pyi | 65 ++++++++++++++++++++++++++++ pandas/_libs/tslibs/fields.pyi | 69 ++++++++++++++++++++++++++++++ pandas/core/arrays/datetimelike.py | 1 + 4 files changed, 176 insertions(+) create mode 100644 pandas/_libs/tslibs/conversion.pyi create mode 100644 pandas/_libs/tslibs/dtypes.pyi create mode 100644 pandas/_libs/tslibs/fields.pyi diff --git a/pandas/_libs/tslibs/conversion.pyi b/pandas/_libs/tslibs/conversion.pyi new file mode 100644 index 0000000000000..6470361542597 --- /dev/null +++ b/pandas/_libs/tslibs/conversion.pyi @@ -0,0 +1,41 @@ +from datetime import ( + datetime, + tzinfo, +) + +import numpy as np + +DT64NS_DTYPE: np.dtype +TD64NS_DTYPE: np.dtype + +class OutOfBoundsTimedelta(ValueError): ... + +def precision_from_unit( + unit: str, +) -> tuple[ + int, # int64_t + int, +]: ... + + +def ensure_datetime64ns( + arr: np.ndarray, # np.ndarray[datetime64[ANY]] + copy: bool = ..., +) -> np.ndarray: ... # np.ndarray[datetime64ns] + + +def ensure_timedelta64ns( + arr: np.ndarray, # np.ndarray[timedelta64[ANY]] + copy: bool = ..., +) -> np.ndarray: ... # np.ndarray[timedelta64ns] + + +def datetime_to_datetime64( + values: np.ndarray, # np.ndarray[object] +) -> tuple[ + np.ndarray, # np.ndarray[dt64ns] + tzinfo | None, +]: ... + + +def localize_pydatetime(dt: datetime, tz: tzinfo | None) -> datetime: ... diff --git a/pandas/_libs/tslibs/dtypes.pyi b/pandas/_libs/tslibs/dtypes.pyi new file mode 100644 index 0000000000000..53752098bafe9 --- /dev/null +++ b/pandas/_libs/tslibs/dtypes.pyi @@ -0,0 +1,65 @@ +from enum import Enum + +from pandas._libs.tslibs.offsets import BaseOffset + +_attrname_to_abbrevs: dict[str, str] +_period_code_map: dict[str, int] + + +class PeriodDtypeBase: + # actually __cinit__ + def __new__(self, code: int): ... + + def freq_group_code(self) -> int: ... + def date_offset(self) -> BaseOffset: ... + + @classmethod + def from_date_offset(cls, offset: BaseOffset) -> PeriodDtypeBase: ... + + +class FreqGroup(Enum): + FR_ANN: int = ... + FR_QTR: int = ... + FR_MTH: int = ... + FR_WK: int = ... + FR_BUS: int = ... + FR_DAY: int = ... + FR_HR: int = ... + FR_MIN: int = ... + FR_SEC: int = ... + FR_MS: int = ... + FR_US: int = ... + FR_NS: int = ... + FR_UND: int = ... + + @staticmethod + def get_freq_group(code: int) -> FreqGroup: ... + + +class Resolution(Enum): + RESO_NS: int = ... + RESO_US: int = ... + RESO_MS: int = ... + RESO_SEC: int = ... + RESO_MIN: int = ... + RESO_HR: int = ... + RESO_DAY: int = ... + RESO_MTH: int = ... + RESO_QTR: int = ... + RESO_YR: int = ... + + def __lt__(self, other: Resolution) -> bool: ... + + def __ge__(self, other: Resolution) -> bool: ... + + @property + def freq_group(self) -> FreqGroup: ... + + @property + def attrname(self) -> str: ... + + @classmethod + def from_attrname(cls, attrname: str) -> Resolution: ... + + @classmethod + def get_reso_from_freq(cls, freq: str) -> Resolution: ... diff --git a/pandas/_libs/tslibs/fields.pyi b/pandas/_libs/tslibs/fields.pyi new file mode 100644 index 0000000000000..22ae156d78b7d --- /dev/null +++ b/pandas/_libs/tslibs/fields.pyi @@ -0,0 +1,69 @@ +import numpy as np + +def build_field_sarray( + dtindex: np.ndarray, # const int64_t[:] +) -> np.ndarray: ... + +def month_position_check(fields, weekdays) -> str | None: ... + +def get_date_name_field( + dtindex: np.ndarray, # const int64_t[:] + field: str, + locale=..., +) -> np.ndarray: ... # np.ndarray[object] + +def get_start_end_field( + dtindex: np.ndarray, # const int64_t[:] + field: str, + freqstr: str | None = ..., + month_kw: int = ... +) -> np.ndarray: ... # np.ndarray[bool] + + +def get_date_field( + dtindex: np.ndarray, # const int64_t[:] + + field: str, +) -> np.ndarray: ... # np.ndarray[in32] + + +def get_timedelta_field( + tdindex: np.ndarray, # const int64_t[:] + field: str, +) -> np.ndarray: ... # np.ndarray[int32] + + +def isleapyear_arr( + years: np.ndarray, +) -> np.ndarray: ... # np.ndarray[bool] + +def build_isocalendar_sarray( + dtindex: np.ndarray, # const int64_t[:] +) -> np.ndarray: ... + + +def get_locale_names(name_type: str, locale: object = None): ... + + +class RoundTo: + @property + def MINUS_INFTY(self) -> int: ... + + @property + def PLUS_INFTY(self) -> int: ... + + @property + def NEAREST_HALF_EVEN(self) -> int: ... + + @property + def NEAREST_HALF_PLUS_INFTY(self) -> int: ... + + @property + def NEAREST_HALF_MINUS_INFTY(self) -> int: ... + + +def round_nsint64( + values: np.ndarray, # np.ndarray[np.int64] + mode: RoundTo, + nanos: int, +) -> np.ndarray: ... # np.ndarray[np.int64] diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 67241a866ef35..cefb9bfa51280 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1727,6 +1727,7 @@ def _round(self, freq, mode, ambiguous, nonexistent): ) values = self.view("i8") + values = cast(np.ndarray, values) nanos = to_offset(freq).nanos result = round_nsint64(values, mode, nanos) result = self._maybe_mask_results(result, fill_value=iNaT)