Skip to content

Commit 71f7c04

Browse files
committed
TYP: stronger typing for unit, as_unit
1 parent 49f4a94 commit 71f7c04

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

pandas/_libs/tslibs/nattype.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ from pandas._libs.tslibs.period import Period
1717
from pandas._typing import (
1818
Frequency,
1919
TimestampNonexistent,
20+
TimeUnit,
2021
)
2122

2223
NaT: NaTType
@@ -180,4 +181,4 @@ class NaTType:
180181
def __floordiv__(self, other: float, /) -> Self: ...
181182
# other
182183
def __hash__(self) -> int: ...
183-
def as_unit(self, unit: str, round_ok: bool = ...) -> NaTType: ...
184+
def as_unit(self, unit: TimeUnit, round_ok: bool = ...) -> NaTType: ...

pandas/_libs/tslibs/timedeltas.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from pandas._libs.tslibs import (
1515
)
1616
from pandas._typing import (
1717
Frequency,
18+
TimeUnit,
1819
npt,
1920
)
2021

@@ -162,5 +163,5 @@ class Timedelta(timedelta):
162163
) -> np.timedelta64: ...
163164
def view(self, dtype: npt.DTypeLike) -> object: ...
164165
@property
165-
def unit(self) -> str: ...
166-
def as_unit(self, unit: str, round_ok: bool = ...) -> Timedelta: ...
166+
def unit(self) -> TimeUnit: ...
167+
def as_unit(self, unit: TimeUnit, round_ok: bool = ...) -> Timedelta: ...

pandas/_libs/tslibs/timestamps.pyi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ from pandas._libs.tslibs import (
2323
Tick,
2424
Timedelta,
2525
)
26-
from pandas._typing import TimestampNonexistent
26+
from pandas._typing import (
27+
TimestampNonexistent,
28+
TimeUnit,
29+
)
2730

2831
_TimeZones: TypeAlias = str | _tzinfo | None | int
2932

@@ -235,5 +238,5 @@ class Timestamp(datetime):
235238
@property
236239
def daysinmonth(self) -> int: ...
237240
@property
238-
def unit(self) -> str: ...
239-
def as_unit(self, unit: str, round_ok: bool = ...) -> Timestamp: ...
241+
def unit(self) -> TimeUnit: ...
242+
def as_unit(self, unit: TimeUnit, round_ok: bool = ...) -> Timestamp: ...

pandas/core/dtypes/dtypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
IntervalClosedType,
8686
Ordered,
8787
Scalar,
88+
TimeUnit,
8889
npt,
8990
type_t,
9091
)
@@ -820,7 +821,7 @@ def _creso(self) -> int:
820821
return abbrev_to_npy_unit(self.unit)
821822

822823
@property
823-
def unit(self) -> str_type:
824+
def unit(self) -> TimeUnit:
824825
"""
825826
The precision of the datetime data.
826827

pandas/core/window/rolling.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Concatenate,
1616
Literal,
1717
Self,
18+
cast,
1819
final,
1920
overload,
2021
)
@@ -95,6 +96,7 @@
9596
NDFrameT,
9697
QuantileInterpolation,
9798
P,
99+
TimeUnit,
98100
T,
99101
WindowingRankType,
100102
npt,
@@ -2001,6 +2003,7 @@ def _validate(self) -> None:
20012003
except TypeError:
20022004
# if not a datetime dtype, eg for empty dataframes
20032005
unit = "ns"
2006+
unit = cast("TimeUnit", unit)
20042007
self._win_freq_i8 = Timedelta(freq.nanos).as_unit(unit)._value
20052008

20062009
# min_periods must be an integer

0 commit comments

Comments
 (0)