Skip to content

Commit 1dfb14c

Browse files
committed
Add as_unit method to Timedelta
1 parent a83501d commit 1dfb14c

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

pandas-stubs/_libs/tslibs/timedeltas.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,6 @@ class Timedelta(timedelta):
392392
def view(self, dtype: npt.DTypeLike = ...) -> object: ...
393393
@property
394394
def unit(self) -> str: ...
395+
def as_unit(
396+
self, unit: Literal["s", "ms", "us", "ns"], round_ok: bool = ...
397+
) -> Self: ...

tests/test_scalars.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ def test_timedelta_properties_methods() -> None:
530530
check(assert_type(td.view(np.int64), object), np.int64)
531531
check(assert_type(td.view("i8"), object), np.int64)
532532

533+
check(assert_type(td.as_unit("s"), pd.Timedelta), pd.Timedelta)
534+
check(assert_type(td.as_unit("ms"), pd.Timedelta), pd.Timedelta)
535+
check(assert_type(td.as_unit("us", round_ok=True), pd.Timedelta), pd.Timedelta)
536+
check(assert_type(td.as_unit("ns", round_ok=False), pd.Timedelta), pd.Timedelta)
537+
533538

534539
def test_timedelta_add_sub() -> None:
535540
td = pd.Timedelta("1 day")

0 commit comments

Comments
 (0)