Skip to content

Commit 6e29a86

Browse files
committed
TYP: unit str->TimeUnit
1 parent 9ffdf03 commit 6e29a86

File tree

6 files changed

+21
-16
lines changed

6 files changed

+21
-16
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
IntervalClosedType,
8989
TimeAmbiguous,
9090
TimeNonexistent,
91+
TimeUnit,
9192
npt,
9293
)
9394

@@ -413,7 +414,7 @@ def _generate_range(
413414
nonexistent: TimeNonexistent = "raise",
414415
inclusive: IntervalClosedType = "both",
415416
*,
416-
unit: str | None = None,
417+
unit: TimeUnit = "ns",
417418
) -> Self:
418419
periods = dtl.validate_periods(periods)
419420
if freq is None and any(x is None for x in [periods, start, end]):

pandas/core/arrays/timedeltas.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
DtypeObj,
7474
NpDtype,
7575
npt,
76+
TimeUnit,
7677
)
7778

7879
from pandas import DataFrame
@@ -275,7 +276,7 @@ def _from_sequence_not_strict(
275276

276277
@classmethod
277278
def _generate_range(
278-
cls, start, end, periods, freq, closed=None, *, unit: str | None = None
279+
cls, start, end, periods, freq, closed=None, *, unit: TimeUnit
279280
) -> Self:
280281
periods = dtl.validate_periods(periods)
281282
if freq is None and any(x is None for x in [periods, start, end]):
@@ -293,11 +294,8 @@ def _generate_range(
293294
if end is not None:
294295
end = Timedelta(end).as_unit("ns")
295296

296-
if unit is not None:
297-
if unit not in ["s", "ms", "us", "ns"]:
298-
raise ValueError("'unit' must be one of 's', 'ms', 'us', 'ns'")
299-
else:
300-
unit = "ns"
297+
if unit not in ["s", "ms", "us", "ns"]:
298+
raise ValueError("'unit' must be one of 's', 'ms', 'us', 'ns'")
301299

302300
if start is not None and unit is not None:
303301
start = start.as_unit(unit, round_ok=False)

pandas/core/indexes/datetimelike.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from pandas._typing import (
7676
Axis,
7777
JoinHow,
78+
TimeUnit,
7879
npt,
7980
)
8081

@@ -434,10 +435,10 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, ABC):
434435
_is_unique = Index.is_unique
435436

436437
@property
437-
def unit(self) -> str:
438+
def unit(self) -> TimeUnit:
438439
return self._data.unit
439440

440-
def as_unit(self, unit: str) -> Self:
441+
def as_unit(self, unit: TimeUnit) -> Self:
441442
"""
442443
Convert to a dtype with the given unit resolution.
443444

pandas/core/indexes/datetimes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
TimeAmbiguous,
6767
TimeNonexistent,
6868
npt,
69+
TimeUnit,
6970
)
7071

7172
from pandas.core.api import (
@@ -852,7 +853,7 @@ def date_range(
852853
name: Hashable | None = None,
853854
inclusive: IntervalClosedType = "both",
854855
*,
855-
unit: str | None = None,
856+
unit: TimeUnit = "ns",
856857
**kwargs,
857858
) -> DatetimeIndex:
858859
"""
@@ -893,7 +894,7 @@ def date_range(
893894
Include boundaries; Whether to set each bound as closed or open.
894895
895896
.. versionadded:: 1.4.0
896-
unit : str, default None
897+
unit : {'s', 'ms', 'us', 'ns'}, default 'ns'
897898
Specify the desired resolution of the result.
898899
899900
.. versionadded:: 2.0.0

pandas/core/indexes/timedeltas.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333

3434
if TYPE_CHECKING:
3535
from pandas._libs import NaTType
36-
from pandas._typing import DtypeObj
36+
from pandas._typing import (
37+
DtypeObj,
38+
TimeUnit,
39+
)
3740

3841

3942
@inherit_names(
@@ -249,7 +252,7 @@ def timedelta_range(
249252
name=None,
250253
closed=None,
251254
*,
252-
unit: str | None = None,
255+
unit: TimeUnit = "ns",
253256
) -> TimedeltaIndex:
254257
"""
255258
Return a fixed frequency TimedeltaIndex with day as the default.
@@ -269,7 +272,7 @@ def timedelta_range(
269272
closed : str, default None
270273
Make the interval closed with respect to the given frequency to
271274
the 'left', 'right', or both sides (None).
272-
unit : str, default None
275+
unit : {'s', 'ms', 'us', 'ns'}, default 'ns'
273276
Specify the desired resolution of the result.
274277
275278
.. versionadded:: 2.0.0

pandas/core/resample.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
TimedeltaConvertibleTypes,
100100
TimeGrouperOrigin,
101101
TimestampConvertibleTypes,
102+
TimeUnit,
102103
npt,
103104
)
104105

@@ -2835,7 +2836,7 @@ def _get_timestamp_range_edges(
28352836
first: Timestamp,
28362837
last: Timestamp,
28372838
freq: BaseOffset,
2838-
unit: str,
2839+
unit: TimeUnit,
28392840
closed: Literal["right", "left"] = "left",
28402841
origin: TimeGrouperOrigin = "start_day",
28412842
offset: Timedelta | None = None,
@@ -2985,7 +2986,7 @@ def _adjust_dates_anchored(
29852986
closed: Literal["right", "left"] = "right",
29862987
origin: TimeGrouperOrigin = "start_day",
29872988
offset: Timedelta | None = None,
2988-
unit: str = "ns",
2989+
unit: TimeUnit = "ns",
29892990
) -> tuple[Timestamp, Timestamp]:
29902991
# First and last offsets should be calculated from the start day to fix an
29912992
# error cause by resampling across multiple days when a one day period is

0 commit comments

Comments
 (0)