-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: some type annotations in core\tools\datetimes.py #34630
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
Changes from 2 commits
1f5bdaa
22c1539
3a9a543
ed0db3b
a9cd392
1055054
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,16 @@ | |
from datetime import datetime | ||
from functools import partial | ||
from itertools import islice | ||
from typing import TYPE_CHECKING, Optional, TypeVar, Union | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Callable, | ||
List, | ||
Optional, | ||
Tuple, | ||
TypeVar, | ||
Union, | ||
overload, | ||
) | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -15,7 +24,7 @@ | |
_guess_datetime_format, | ||
) | ||
from pandas._libs.tslibs.strptime import array_strptime | ||
from pandas._typing import ArrayLike | ||
from pandas._typing import ArrayLike, Label | ||
|
||
from pandas.core.dtypes.common import ( | ||
ensure_object, | ||
|
@@ -49,12 +58,10 @@ | |
# --------------------------------------------------------------------- | ||
# types used in annotations | ||
|
||
ArrayConvertible = Union[list, tuple, ArrayLike, "Series"] | ||
ArrayConvertible = Union[List, Tuple, ArrayLike, "Series"] | ||
Scalar = Union[int, float, str] | ||
DatetimeScalar = TypeVar("DatetimeScalar", Scalar, datetime) | ||
DatetimeScalarOrArrayConvertible = Union[ | ||
DatetimeScalar, list, tuple, ArrayLike, "Series" | ||
] | ||
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible] | ||
|
||
|
||
# --------------------------------------------------------------------- | ||
|
@@ -123,7 +130,12 @@ def should_cache( | |
return do_caching | ||
|
||
|
||
def _maybe_cache(arg, format, cache, convert_listlike): | ||
def _maybe_cache( | ||
arg: ArrayConvertible, | ||
format: Optional[str], | ||
cache: bool, | ||
convert_listlike: Callable, | ||
) -> "Series": | ||
""" | ||
Create a cache of unique dates from an array of dates | ||
|
||
|
@@ -159,7 +171,7 @@ def _maybe_cache(arg, format, cache, convert_listlike): | |
|
||
|
||
def _box_as_indexlike( | ||
dt_array: ArrayLike, utc: Optional[bool] = None, name: Optional[str] = None | ||
dt_array: ArrayLike, utc: Optional[bool] = None, name: Label = None | ||
) -> Index: | ||
""" | ||
Properly boxes the ndarray of datetimes to DatetimeIndex | ||
|
@@ -244,15 +256,15 @@ def _return_parsed_timezone_results(result, timezones, tz, name): | |
|
||
def _convert_listlike_datetimes( | ||
arg, | ||
format, | ||
name=None, | ||
tz=None, | ||
unit=None, | ||
errors=None, | ||
infer_datetime_format=None, | ||
dayfirst=None, | ||
yearfirst=None, | ||
exact=None, | ||
format: Optional[str], | ||
name: Label = None, | ||
tz: Optional[str] = None, | ||
unit: Optional[str] = None, | ||
errors: Optional[str] = None, | ||
infer_datetime_format: Optional[bool] = None, | ||
dayfirst: Optional[bool] = None, | ||
yearfirst: Optional[bool] = None, | ||
exact: Optional[bool] = None, | ||
): | ||
""" | ||
Helper function for to_datetime. Performs the conversions of 1D listlike | ||
|
@@ -539,19 +551,70 @@ def _adjust_to_origin(arg, origin, unit): | |
return arg | ||
|
||
|
||
@overload | ||
def to_datetime( | ||
arg, | ||
errors="raise", | ||
dayfirst=False, | ||
yearfirst=False, | ||
utc=None, | ||
format=None, | ||
exact=True, | ||
unit=None, | ||
infer_datetime_format=False, | ||
arg: DatetimeScalar, | ||
errors: str = ..., | ||
dayfirst: bool = ..., | ||
yearfirst: bool = ..., | ||
utc: Optional[bool] = ..., | ||
format: Optional[str] = ..., | ||
exact: bool = ..., | ||
unit: Optional[str] = ..., | ||
infer_datetime_format: bool = ..., | ||
origin=..., | ||
cache: bool = ..., | ||
) -> Timestamp: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or NaT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type here is dependent of the value of the errors parameter. so to refine the overload further requires Literal. updated to a Union return type with NaT but needs a cast (or ignore/assert) in pandas/io/excel/_odfreader.py also changed from Timestamp to DatetimeScalar since with error="ignore" returns arg value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
FWIW I think (separate to this PR) that we will need to start vendoring parts of typing_extensions and this would be a good candidate. I also think Protocol is necessary for us to unpin where we are at now particularly with our mixin classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for vendoring typing_extensions |
||
... | ||
|
||
|
||
@overload | ||
def to_datetime( | ||
arg: "Series", | ||
errors: str = ..., | ||
dayfirst: bool = ..., | ||
yearfirst: bool = ..., | ||
utc: Optional[bool] = ..., | ||
format: Optional[str] = ..., | ||
exact: bool = ..., | ||
unit: Optional[str] = ..., | ||
infer_datetime_format: bool = ..., | ||
origin=..., | ||
cache: bool = ..., | ||
) -> "Series": | ||
... | ||
|
||
|
||
@overload | ||
def to_datetime( | ||
arg: Union[List, Tuple], | ||
errors: str = ..., | ||
dayfirst: bool = ..., | ||
yearfirst: bool = ..., | ||
utc: Optional[bool] = ..., | ||
format: Optional[str] = ..., | ||
exact: bool = ..., | ||
unit: Optional[str] = ..., | ||
infer_datetime_format: bool = ..., | ||
origin=..., | ||
cache: bool = ..., | ||
) -> DatetimeIndex: | ||
... | ||
|
||
|
||
def to_datetime( | ||
arg: DatetimeScalarOrArrayConvertible, | ||
errors: str = "raise", | ||
dayfirst: bool = False, | ||
yearfirst: bool = False, | ||
utc: Optional[bool] = None, | ||
format: Optional[str] = None, | ||
exact: bool = True, | ||
unit: Optional[str] = None, | ||
infer_datetime_format: bool = False, | ||
origin="unix", | ||
cache=True, | ||
): | ||
cache: bool = True, | ||
) -> Union[DatetimeIndex, "Series", Timestamp]: | ||
""" | ||
Convert argument to datetime. | ||
|
||
|
@@ -746,8 +809,7 @@ def to_datetime( | |
if not cache_array.empty: | ||
result = _convert_and_box_cache(arg, cache_array, name=arg.name) | ||
else: | ||
convert_listlike = partial(convert_listlike, name=arg.name) | ||
result = convert_listlike(arg, format) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for avoiding the partial here |
||
result = convert_listlike(arg, format, name=arg.name) | ||
elif is_list_like(arg): | ||
try: | ||
cache_array = _maybe_cache(arg, format, cache, convert_listlike) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,8 +255,8 @@ def __add__(self, other): | |
pd.array([1, 3, 2], dtype="int64"), | ||
pd.array([1, 10, 0], dtype="Sparse[int]"), | ||
pd.to_datetime(["2000", "2010", "2001"]), | ||
pd.to_datetime(["2000", "2010", "2001"]).tz_localize("CET"), | ||
pd.to_datetime(["2000", "2010", "2001"]).to_period(freq="D"), | ||
pd.to_datetime(["2000", "2010", "2001"]).tz_localize("CET"), # type: ignore | ||
pd.to_datetime(["2000", "2010", "2001"]).to_period(freq="D"), # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC this happens bc we use inherit_names on DatetimeIndex. I'd be OK with pinning the inherited methods in a way mypy gets even if its a little more verbose if you think its worthwhile |
||
], | ||
) | ||
def test_reduce(values): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tz could be a tzinfo? might merit a type in pd._typing corresponding to "tzinfo or str that we can convert to one"