Skip to content

Commit d00763a

Browse files
authored
TYP: expand acceptable types for pd.to_datetime() (pandas-dev#46273)
1 parent 8561eb5 commit d00763a

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

pandas/core/tools/datetimes.py

+37-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Hashable,
1111
List,
1212
Tuple,
13+
TypedDict,
1314
Union,
1415
cast,
1516
overload,
@@ -79,16 +80,42 @@
7980
if TYPE_CHECKING:
8081
from pandas._libs.tslibs.nattype import NaTType
8182

82-
from pandas import Series
83+
from pandas import (
84+
DataFrame,
85+
Series,
86+
)
8387

8488
# ---------------------------------------------------------------------
8589
# types used in annotations
8690

87-
ArrayConvertible = Union[List, Tuple, AnyArrayLike, "Series"]
91+
ArrayConvertible = Union[List, Tuple, AnyArrayLike]
8892
Scalar = Union[int, float, str]
8993
DatetimeScalar = Union[Scalar, datetime]
9094

9195
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible]
96+
97+
DatetimeDictArg = Union[List[Scalar], Tuple[Scalar, ...], AnyArrayLike]
98+
99+
100+
class YearMonthDayDict(TypedDict, total=True):
101+
year: DatetimeDictArg
102+
month: DatetimeDictArg
103+
day: DatetimeDictArg
104+
105+
106+
class FulldatetimeDict(YearMonthDayDict, total=False):
107+
hour: DatetimeDictArg
108+
hours: DatetimeDictArg
109+
minute: DatetimeDictArg
110+
minutes: DatetimeDictArg
111+
second: DatetimeDictArg
112+
seconds: DatetimeDictArg
113+
ms: DatetimeDictArg
114+
us: DatetimeDictArg
115+
ns: DatetimeDictArg
116+
117+
118+
DictConvertible = Union[FulldatetimeDict, "DataFrame"]
92119
start_caching_at = 50
93120

94121

@@ -640,13 +667,13 @@ def to_datetime(
640667
infer_datetime_format: bool = ...,
641668
origin=...,
642669
cache: bool = ...,
643-
) -> Timestamp | NaTType:
670+
) -> Timestamp:
644671
...
645672

646673

647674
@overload
648675
def to_datetime(
649-
arg: Series,
676+
arg: Series | DictConvertible,
650677
errors: str = ...,
651678
dayfirst: bool = ...,
652679
yearfirst: bool = ...,
@@ -663,7 +690,7 @@ def to_datetime(
663690

664691
@overload
665692
def to_datetime(
666-
arg: list | tuple | np.ndarray,
693+
arg: list | tuple | Index | ArrayLike,
667694
errors: str = ...,
668695
dayfirst: bool = ...,
669696
yearfirst: bool = ...,
@@ -679,7 +706,7 @@ def to_datetime(
679706

680707

681708
def to_datetime(
682-
arg: DatetimeScalarOrArrayConvertible,
709+
arg: DatetimeScalarOrArrayConvertible | DictConvertible,
683710
errors: str = "raise",
684711
dayfirst: bool = False,
685712
yearfirst: bool = False,
@@ -1067,10 +1094,10 @@ def to_datetime(
10671094
# "Union[float, str, datetime, List[Any], Tuple[Any, ...], ExtensionArray,
10681095
# ndarray[Any, Any], Series]"; expected "Union[List[Any], Tuple[Any, ...],
10691096
# Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series], Series]"
1070-
arg = cast(
1097+
argc = cast(
10711098
Union[list, tuple, ExtensionArray, np.ndarray, "Series", Index], arg
10721099
)
1073-
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
1100+
cache_array = _maybe_cache(argc, format, cache, convert_listlike)
10741101
except OutOfBoundsDatetime:
10751102
# caching attempts to create a DatetimeIndex, which may raise
10761103
# an OOB. If that's the desired behavior, then just reraise...
@@ -1081,9 +1108,9 @@ def to_datetime(
10811108

10821109
cache_array = Series([], dtype=object) # just an empty array
10831110
if not cache_array.empty:
1084-
result = _convert_and_box_cache(arg, cache_array)
1111+
result = _convert_and_box_cache(argc, cache_array)
10851112
else:
1086-
result = convert_listlike(arg, format)
1113+
result = convert_listlike(argc, format)
10871114
else:
10881115
result = convert_listlike(np.array([arg]), format)[0]
10891116
if isinstance(arg, bool) and isinstance(result, np.bool_):

pandas/io/excel/_odfreader.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ def _get_cell_value(self, cell, convert_float: bool) -> Scalar | NaTType:
214214
cell_value = cell.attributes.get((OFFICENS, "date-value"))
215215
return pd.to_datetime(cell_value)
216216
elif cell_type == "time":
217-
# cast needed because `pd.to_datetime can return NaTType,
218-
# but we know this is a valid time
219-
stamp = cast(pd.Timestamp, pd.to_datetime(str(cell)))
217+
stamp = pd.to_datetime(str(cell))
220218
# cast needed here because Scalar doesn't include datetime.time
221219
return cast(Scalar, stamp.time())
222220
else:

0 commit comments

Comments
 (0)