timedeltas.pyi 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from datetime import timedelta
  2. from typing import (
  3. ClassVar,
  4. Literal,
  5. TypeAlias,
  6. TypeVar,
  7. overload,
  8. )
  9. import numpy as np
  10. from pandas._libs.tslibs import (
  11. NaTType,
  12. Tick,
  13. )
  14. from pandas._typing import (
  15. Frequency,
  16. Self,
  17. npt,
  18. )
  19. # This should be kept consistent with the keys in the dict timedelta_abbrevs
  20. # in pandas/_libs/tslibs/timedeltas.pyx
  21. UnitChoices: TypeAlias = Literal[
  22. "Y",
  23. "y",
  24. "M",
  25. "W",
  26. "w",
  27. "D",
  28. "d",
  29. "days",
  30. "day",
  31. "hours",
  32. "hour",
  33. "hr",
  34. "h",
  35. "m",
  36. "minute",
  37. "min",
  38. "minutes",
  39. "T",
  40. "t",
  41. "s",
  42. "seconds",
  43. "sec",
  44. "second",
  45. "ms",
  46. "milliseconds",
  47. "millisecond",
  48. "milli",
  49. "millis",
  50. "L",
  51. "l",
  52. "us",
  53. "microseconds",
  54. "microsecond",
  55. "µs",
  56. "micro",
  57. "micros",
  58. "u",
  59. "ns",
  60. "nanoseconds",
  61. "nano",
  62. "nanos",
  63. "nanosecond",
  64. "n",
  65. ]
  66. _S = TypeVar("_S", bound=timedelta)
  67. def get_unit_for_round(freq, creso: int) -> int: ...
  68. def disallow_ambiguous_unit(unit: str | None) -> None: ...
  69. def ints_to_pytimedelta(
  70. m8values: npt.NDArray[np.timedelta64],
  71. box: bool = ...,
  72. ) -> npt.NDArray[np.object_]: ...
  73. def array_to_timedelta64(
  74. values: npt.NDArray[np.object_],
  75. unit: str | None = ...,
  76. errors: str = ...,
  77. ) -> np.ndarray: ... # np.ndarray[m8ns]
  78. def parse_timedelta_unit(unit: str | None) -> UnitChoices: ...
  79. def delta_to_nanoseconds(
  80. delta: np.timedelta64 | timedelta | Tick,
  81. reso: int = ..., # NPY_DATETIMEUNIT
  82. round_ok: bool = ...,
  83. ) -> int: ...
  84. def floordiv_object_array(
  85. left: np.ndarray, right: npt.NDArray[np.object_]
  86. ) -> np.ndarray: ...
  87. def truediv_object_array(
  88. left: np.ndarray, right: npt.NDArray[np.object_]
  89. ) -> np.ndarray: ...
  90. class Timedelta(timedelta):
  91. _creso: int
  92. min: ClassVar[Timedelta]
  93. max: ClassVar[Timedelta]
  94. resolution: ClassVar[Timedelta]
  95. value: int # np.int64
  96. _value: int # np.int64
  97. # error: "__new__" must return a class instance (got "Union[Timestamp, NaTType]")
  98. def __new__( # type: ignore[misc]
  99. cls: type[_S],
  100. value=...,
  101. unit: str | None = ...,
  102. **kwargs: float | np.integer | np.floating,
  103. ) -> _S | NaTType: ...
  104. @classmethod
  105. def _from_value_and_reso(cls, value: np.int64, reso: int) -> Timedelta: ...
  106. @property
  107. def days(self) -> int: ...
  108. @property
  109. def seconds(self) -> int: ...
  110. @property
  111. def microseconds(self) -> int: ...
  112. def total_seconds(self) -> float: ...
  113. def to_pytimedelta(self) -> timedelta: ...
  114. def to_timedelta64(self) -> np.timedelta64: ...
  115. @property
  116. def asm8(self) -> np.timedelta64: ...
  117. # TODO: round/floor/ceil could return NaT?
  118. def round(self, freq: Frequency) -> Self: ...
  119. def floor(self, freq: Frequency) -> Self: ...
  120. def ceil(self, freq: Frequency) -> Self: ...
  121. @property
  122. def resolution_string(self) -> str: ...
  123. def __add__(self, other: timedelta) -> Timedelta: ...
  124. def __radd__(self, other: timedelta) -> Timedelta: ...
  125. def __sub__(self, other: timedelta) -> Timedelta: ...
  126. def __rsub__(self, other: timedelta) -> Timedelta: ...
  127. def __neg__(self) -> Timedelta: ...
  128. def __pos__(self) -> Timedelta: ...
  129. def __abs__(self) -> Timedelta: ...
  130. def __mul__(self, other: float) -> Timedelta: ...
  131. def __rmul__(self, other: float) -> Timedelta: ...
  132. # error: Signature of "__floordiv__" incompatible with supertype "timedelta"
  133. @overload # type: ignore[override]
  134. def __floordiv__(self, other: timedelta) -> int: ...
  135. @overload
  136. def __floordiv__(self, other: float) -> Timedelta: ...
  137. @overload
  138. def __floordiv__(
  139. self, other: npt.NDArray[np.timedelta64]
  140. ) -> npt.NDArray[np.intp]: ...
  141. @overload
  142. def __floordiv__(
  143. self, other: npt.NDArray[np.number]
  144. ) -> npt.NDArray[np.timedelta64] | Timedelta: ...
  145. @overload
  146. def __rfloordiv__(self, other: timedelta | str) -> int: ...
  147. @overload
  148. def __rfloordiv__(self, other: None | NaTType) -> NaTType: ...
  149. @overload
  150. def __rfloordiv__(self, other: np.ndarray) -> npt.NDArray[np.timedelta64]: ...
  151. @overload
  152. def __truediv__(self, other: timedelta) -> float: ...
  153. @overload
  154. def __truediv__(self, other: float) -> Timedelta: ...
  155. def __mod__(self, other: timedelta) -> Timedelta: ...
  156. def __divmod__(self, other: timedelta) -> tuple[int, Timedelta]: ...
  157. def __le__(self, other: timedelta) -> bool: ...
  158. def __lt__(self, other: timedelta) -> bool: ...
  159. def __ge__(self, other: timedelta) -> bool: ...
  160. def __gt__(self, other: timedelta) -> bool: ...
  161. def __hash__(self) -> int: ...
  162. def isoformat(self) -> str: ...
  163. def to_numpy(
  164. self, dtype: npt.DTypeLike = ..., copy: bool = False
  165. ) -> np.timedelta64: ...
  166. def view(self, dtype: npt.DTypeLike) -> object: ...
  167. @property
  168. def unit(self) -> str: ...
  169. def as_unit(self, unit: str, round_ok: bool = ...) -> Timedelta: ...