Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to_timedelta argument type from list to Sequence #959

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas-stubs/core/tools/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def to_timedelta(
@overload
def to_timedelta(
arg: (
Sequence[float | timedelta]
Sequence[str | float | timedelta]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI is failing because a plain string will match Sequence[str] . So you should change this to

Suggested change
Sequence[str | float | timedelta]
SequenceNotStr | Sequence[float | timedelta]

You can import SequenceNotStr from _typing

Also, you should set up your environment and test locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that you are confirming my initial solution, '1 day' and ['1 day'] are both Sequence.
I forgot to rerun locally when I pushed the feedback last time, should be good now.
Thanks for all the insights on my first PR here!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to force the typehint in the test, let me know if this is in the spirit.

| list[str | float | timedelta]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine these to one item of Sequence[str | float | timedelta]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed Sequence is encompassing list so might as well.

| tuple[str | float | timedelta, ...]
| range
Expand Down
9 changes: 9 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need these tests here.

You do need to do the test as follows:

check(assert_type(pd.to_timedelta(["1 day"]), pd.TimedeltaIndex), pd.TimedeltaIndex)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback and the clarity!



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)
Expand Down