Skip to content

Commit 84d9c5e

Browse files
authored
TYP: timedeltas.pyi (#40766)
1 parent ce6162b commit 84d9c5e

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed

pandas/_libs/tslibs/timedeltas.pyi

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from datetime import timedelta
2+
from typing import (
3+
ClassVar,
4+
Type,
5+
TypeVar,
6+
overload,
7+
)
8+
9+
import numpy as np
10+
11+
from pandas._libs.tslibs import (
12+
NaTType,
13+
Tick,
14+
)
15+
16+
_S = TypeVar("_S")
17+
18+
19+
def ints_to_pytimedelta(
20+
arr: np.ndarray, # const int64_t[:]
21+
box: bool = ...,
22+
) -> np.ndarray: ... # np.ndarray[object]
23+
24+
25+
def array_to_timedelta64(
26+
values: np.ndarray, # ndarray[object]
27+
unit: str | None = ...,
28+
errors: str = ...,
29+
) -> np.ndarray: ... # np.ndarray[m8ns]
30+
31+
32+
def parse_timedelta_unit(unit: str | None) -> str: ...
33+
34+
35+
def delta_to_nanoseconds(delta: Tick | np.timedelta64 | timedelta | int) -> int: ...
36+
37+
38+
class Timedelta(timedelta):
39+
min: ClassVar[Timedelta]
40+
max: ClassVar[Timedelta]
41+
resolution: ClassVar[Timedelta]
42+
value: int # np.int64
43+
44+
# error: "__new__" must return a class instance (got "Union[Timedelta, NaTType]")
45+
def __new__( # type: ignore[misc]
46+
cls: Type[_S],
47+
value=...,
48+
unit=...,
49+
**kwargs
50+
) -> _S | NaTType: ...
51+
52+
@property
53+
def days(self) -> int: ...
54+
@property
55+
def seconds(self) -> int: ...
56+
@property
57+
def microseconds(self) -> int: ...
58+
def total_seconds(self) -> float: ...
59+
60+
def to_pytimedelta(self) -> timedelta: ...
61+
def to_timedelta64(self) -> np.timedelta64: ...
62+
63+
@property
64+
def asm8(self) -> np.timedelta64: ...
65+
66+
# TODO: round/floor/ceil could return NaT?
67+
def round(self: _S, freq) -> _S: ...
68+
def floor(self: _S, freq) -> _S: ...
69+
def ceil(self: _S, freq) -> _S: ...
70+
71+
@property
72+
def resolution_string(self) -> str: ...
73+
74+
def __add__(self, other: timedelta) -> timedelta: ...
75+
def __radd__(self, other: timedelta) -> timedelta: ...
76+
def __sub__(self, other: timedelta) -> timedelta: ...
77+
def __rsub__(self, other: timedelta) -> timedelta: ...
78+
def __neg__(self) -> timedelta: ...
79+
def __pos__(self) -> timedelta: ...
80+
def __abs__(self) -> timedelta: ...
81+
def __mul__(self, other: float) -> timedelta: ...
82+
def __rmul__(self, other: float) -> timedelta: ...
83+
84+
@overload
85+
def __floordiv__(self, other: timedelta) -> int: ...
86+
@overload
87+
def __floordiv__(self, other: int) -> timedelta: ...
88+
89+
@overload
90+
def __truediv__(self, other: timedelta) -> float: ...
91+
@overload
92+
def __truediv__(self, other: float) -> timedelta: ...
93+
def __mod__(self, other: timedelta) -> timedelta: ...
94+
def __divmod__(self, other: timedelta) -> tuple[int, timedelta]: ...
95+
96+
def __le__(self, other: timedelta) -> bool: ...
97+
def __lt__(self, other: timedelta) -> bool: ...
98+
def __ge__(self, other: timedelta) -> bool: ...
99+
def __gt__(self, other: timedelta) -> bool: ...
100+
def __hash__(self) -> int: ...

pandas/core/dtypes/cast.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,15 @@ def maybe_box_native(value: Scalar) -> Scalar:
191191
if is_datetime_or_timedelta_dtype(value):
192192
value = maybe_box_datetimelike(value)
193193
elif is_float(value):
194-
value = float(value)
194+
# error: Argument 1 to "float" has incompatible type
195+
# "Union[Union[str, int, float, bool], Union[Any, Any, Timedelta, Any]]";
196+
# expected "Union[SupportsFloat, _SupportsIndex, str]"
197+
value = float(value) # type: ignore[arg-type]
195198
elif is_integer(value):
196-
value = int(value)
199+
# error: Argument 1 to "int" has incompatible type
200+
# "Union[Union[str, int, float, bool], Union[Any, Any, Timedelta, Any]]";
201+
# pected "Union[str, SupportsInt, _SupportsIndex, _SupportsTrunc]"
202+
value = int(value) # type: ignore[arg-type]
197203
elif is_bool(value):
198204
value = bool(value)
199205
return value
@@ -2107,10 +2113,15 @@ def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
21072113
------
21082114
ValueError
21092115
"""
2116+
# error: Argument 1 to "__call__" of "ufunc" has incompatible type
2117+
# "Union[Union[str, int, float, bool], Union[Any, Any, Timedelta, Any]]";
2118+
# expected "Union[Union[int, float, complex, str, bytes, generic],
2119+
# Sequence[Union[int, float, complex, str, bytes, generic]],
2120+
# Sequence[Sequence[Any]], _SupportsArray]"
21102121
if (
21112122
issubclass(dtype.type, (np.integer, np.bool_))
21122123
and is_float(value)
2113-
and np.isnan(value)
2124+
and np.isnan(value) # type: ignore[arg-type]
21142125
):
21152126
raise ValueError("Cannot assign nan to integer series")
21162127

pandas/core/reshape/tile.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"""
22
Quantilization functions and related stuff
33
"""
4+
from typing import (
5+
Any,
6+
Callable,
7+
)
8+
49
import numpy as np
510

611
from pandas._libs import (
@@ -550,6 +555,8 @@ def _format_labels(
550555
""" based on the dtype, return our labels """
551556
closed = "right" if right else "left"
552557

558+
formatter: Callable[[Any], Timestamp] | Callable[[Any], Timedelta]
559+
553560
if is_datetime64tz_dtype(dtype):
554561
formatter = lambda x: Timestamp(x, tz=dtype.tz)
555562
adjust = lambda x: x - Timedelta("1ns")

pandas/core/tools/timedeltas.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import numpy as np
66

77
from pandas._libs import lib
8-
from pandas._libs.tslibs import NaT
8+
from pandas._libs.tslibs import (
9+
NaT,
10+
NaTType,
11+
)
912
from pandas._libs.tslibs.timedeltas import (
1013
Timedelta,
1114
parse_timedelta_unit,
@@ -141,6 +144,8 @@ def to_timedelta(arg, unit=None, errors="raise"):
141144

142145
def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"):
143146
"""Convert string 'r' to a timedelta object."""
147+
result: Timedelta | NaTType # TODO: alias?
148+
144149
try:
145150
result = Timedelta(r, unit)
146151
except ValueError:

0 commit comments

Comments
 (0)