From 36dfe7f7814f2c4b3c92fac8ffb02702e71b3720 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Fri, 12 Jul 2024 16:45:28 -0700 Subject: [PATCH 1/6] GH 956 Allowing sequence type in to_timedelta --- pandas-stubs/core/tools/timedeltas.pyi | 2 +- tests/test_timefuncs.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas-stubs/core/tools/timedeltas.pyi b/pandas-stubs/core/tools/timedeltas.pyi index aa2c34ac..86920456 100644 --- a/pandas-stubs/core/tools/timedeltas.pyi +++ b/pandas-stubs/core/tools/timedeltas.pyi @@ -31,7 +31,7 @@ def to_timedelta( @overload def to_timedelta( arg: ( - Sequence[float | timedelta] + Sequence[str | float | timedelta] | list[str | float | timedelta] | tuple[str | float | timedelta, ...] | range diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index e3dcb9a8..7de8d4b1 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -140,6 +140,15 @@ def test_timedelta_series_arithmetic() -> None: r4: pd.TimedeltaIndex = tds1 / 10.2 +def test_timedelta_series_string() -> None: + tds1: pd.TimedeltaIndex = pd.to_timedelta(["1 day"]) + td1: pd.Timedelta = pd.Timedelta("2 days") + r1: pd.TimedeltaIndex = tds1 + td1 + r2: pd.TimedeltaIndex = tds1 - td1 + r3: pd.TimedeltaIndex = tds1 * 4.3 + r4: pd.TimedeltaIndex = tds1 / 10.2 + + def test_timestamp_timedelta_series_arithmetic() -> None: ts1 = pd.to_datetime(pd.Series(["2022-03-05", "2022-03-06"])) assert isinstance(ts1.iloc[0], pd.Timestamp) From 485ddd0ccfd9be135e14cc8c6b944ef6b6fbd45a Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 15 Jul 2024 11:19:51 -0700 Subject: [PATCH 2/6] GH 956 PR feedback --- pandas-stubs/core/tools/timedeltas.pyi | 1 - tests/test_timefuncs.py | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/pandas-stubs/core/tools/timedeltas.pyi b/pandas-stubs/core/tools/timedeltas.pyi index 86920456..41975bcd 100644 --- a/pandas-stubs/core/tools/timedeltas.pyi +++ b/pandas-stubs/core/tools/timedeltas.pyi @@ -32,7 +32,6 @@ def to_timedelta( def to_timedelta( arg: ( Sequence[str | float | timedelta] - | list[str | float | timedelta] | tuple[str | float | timedelta, ...] | range | ArrayLike diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 7de8d4b1..50972039 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -141,12 +141,7 @@ def test_timedelta_series_arithmetic() -> None: def test_timedelta_series_string() -> None: - tds1: pd.TimedeltaIndex = pd.to_timedelta(["1 day"]) - td1: pd.Timedelta = pd.Timedelta("2 days") - r1: pd.TimedeltaIndex = tds1 + td1 - r2: pd.TimedeltaIndex = tds1 - td1 - r3: pd.TimedeltaIndex = tds1 * 4.3 - r4: pd.TimedeltaIndex = tds1 / 10.2 + check(assert_type(pd.to_timedelta(["1 day"]), pd.TimedeltaIndex), pd.TimedeltaIndex) def test_timestamp_timedelta_series_arithmetic() -> None: From a143dcd9cbf713dbed5eb4da4b0e939975a47339 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 15 Jul 2024 13:46:33 -0700 Subject: [PATCH 3/6] GH 956 PR feedback --- pandas-stubs/core/tools/timedeltas.pyi | 4 +++- tests/test_timefuncs.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas-stubs/core/tools/timedeltas.pyi b/pandas-stubs/core/tools/timedeltas.pyi index 41975bcd..ea0a38a0 100644 --- a/pandas-stubs/core/tools/timedeltas.pyi +++ b/pandas-stubs/core/tools/timedeltas.pyi @@ -14,6 +14,7 @@ from pandas._libs.tslibs.timedeltas import TimeDeltaUnitChoices from pandas._typing import ( ArrayLike, RaiseCoerce, + SequenceNotStr, ) @overload @@ -31,7 +32,8 @@ def to_timedelta( @overload def to_timedelta( arg: ( - Sequence[str | float | timedelta] + SequenceNotStr[str] + | Sequence[float | timedelta] | tuple[str | float | timedelta, ...] | range | ArrayLike diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 50972039..800c954d 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -25,7 +25,10 @@ from pandas._typing import FulldatetimeDict else: FulldatetimeDict = Any -from pandas._typing import TimeUnit +from pandas._typing import ( + SequenceNotStr, + TimeUnit, +) from tests import ( PD_LTE_22, @@ -141,7 +144,8 @@ def test_timedelta_series_arithmetic() -> None: def test_timedelta_series_string() -> None: - check(assert_type(pd.to_timedelta(["1 day"]), pd.TimedeltaIndex), pd.TimedeltaIndex) + seq_list: SequenceNotStr[str] = ["1 day"] + check(assert_type(pd.to_timedelta(seq_list), pd.TimedeltaIndex), pd.TimedeltaIndex) def test_timestamp_timedelta_series_arithmetic() -> None: From cffcec62a5765f903058353796d8f9857cdebeea Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 15 Jul 2024 13:49:55 -0700 Subject: [PATCH 4/6] GH 956 PR feedback --- pandas-stubs/core/tools/timedeltas.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas-stubs/core/tools/timedeltas.pyi b/pandas-stubs/core/tools/timedeltas.pyi index ea0a38a0..38ed0c07 100644 --- a/pandas-stubs/core/tools/timedeltas.pyi +++ b/pandas-stubs/core/tools/timedeltas.pyi @@ -32,7 +32,7 @@ def to_timedelta( @overload def to_timedelta( arg: ( - SequenceNotStr[str] + SequenceNotStr | Sequence[float | timedelta] | tuple[str | float | timedelta, ...] | range From e6fb19cd6cd93f6d85e053c6fe4197d321d8d77e Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 15 Jul 2024 16:40:27 -0700 Subject: [PATCH 5/6] Update tests/test_timefuncs.py Co-authored-by: Irv Lustig --- tests/test_timefuncs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 800c954d..f29ca9f6 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -144,7 +144,7 @@ def test_timedelta_series_arithmetic() -> None: def test_timedelta_series_string() -> None: - seq_list: SequenceNotStr[str] = ["1 day"] + seq_list = ["1 day"] check(assert_type(pd.to_timedelta(seq_list), pd.TimedeltaIndex), pd.TimedeltaIndex) From 5eff82e25fa8322a0b9ec5d9a798bdf1306d3ca7 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 15 Jul 2024 16:45:01 -0700 Subject: [PATCH 6/6] Fixing lint issue --- tests/test_timefuncs.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index f29ca9f6..a61bb92c 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -25,10 +25,7 @@ from pandas._typing import FulldatetimeDict else: FulldatetimeDict = Any -from pandas._typing import ( - SequenceNotStr, - TimeUnit, -) +from pandas._typing import TimeUnit from tests import ( PD_LTE_22,