Skip to content

TYP: expand acceptable types for pd.to_datetime() #46273

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

Merged
merged 21 commits into from
Mar 11, 2022

Conversation

Dr-Irv
Copy link
Contributor

@Dr-Irv Dr-Irv commented Mar 8, 2022

We document that pd.to_datetime() can accept a DataFrame or "dict-like" as an argument, but the overloads did not specify that. This PR adds that.

In addition, there was no overload for Index or ExtensionArray as an input type that converts to DatetimeIndex

Also, changed the overload for pd.to_datetime() on a scalar to only return Timestamp to make user code easier to write. E.g.,

import pandas as pd
ts: pd.Timestamp = pd.to_datetime("2022-03-05")

with the current version where pd.to_datetime() can return Timestamp | NaTType, users would have to do a cast

@Dr-Irv Dr-Irv requested a review from jbrockmendel March 8, 2022 19:41
@@ -663,7 +668,7 @@ def to_datetime(

@overload
def to_datetime(
arg: list | tuple | np.ndarray,
arg: list | tuple | np.ndarray | Index | ExtensionArray,
Copy link
Member

Choose a reason for hiding this comment

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

Sequence?

Copy link
Contributor Author

@Dr-Irv Dr-Irv Mar 9, 2022

Choose a reason for hiding this comment

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

No, can't use Sequence, because a string is a sequence, and that has to match the scalar type above this.

Keeping unchanged.

Copy link
Member

Choose a reason for hiding this comment

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

Could replace np.ndarray | ExtensionArray with ArrayLike.

about Sequence and str: Does to_datetime require certain list/tuple elements? I'm sure that str isn't part of Sequence[datetime.time]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to_datetime can take mixed lists/tuples of different kinds of scalars (datetimes, ints, floats, strings at a minimum), and I'm a bit hesitant to figure them all out to avoid making this too narrow. So I'm leaving this as list | tuple for now.

@twoertwein twoertwein added the Typing type annotations, mypy/pyright type checking label Mar 10, 2022
Copy link
Member

@twoertwein twoertwein left a comment

Choose a reason for hiding this comment

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

Small comments, otherwise looks good to me (still not a great fan of removing NaTType but it makes it consistent with Timestamp/Timedelta).

@jreback jreback added this to the 1.5 milestone Mar 11, 2022
@jreback jreback merged commit d00763a into pandas-dev:main Mar 11, 2022
@jreback
Copy link
Contributor

jreback commented Mar 11, 2022

thanks @Dr-Irv

yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
@Dr-Irv Dr-Irv deleted the typetodatetime branch February 13, 2023 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants