Skip to content

Commit 8c1a507

Browse files
authored
REF: re-use sequence_to_datetimes in try_datetime (#40191)
1 parent 100dd37 commit 8c1a507

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

pandas/core/arrays/datetimes.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1905,13 +1905,16 @@ def std(
19051905

19061906

19071907
def sequence_to_datetimes(
1908-
data, allow_object: bool = False
1908+
data, allow_object: bool = False, require_iso8601: bool = False
19091909
) -> Union[np.ndarray, DatetimeArray]:
19101910
"""
19111911
Parse/convert the passed data to either DatetimeArray or np.ndarray[object].
19121912
"""
19131913
result, tz, freq = sequence_to_dt64ns(
1914-
data, allow_object=allow_object, allow_mixed=True
1914+
data,
1915+
allow_object=allow_object,
1916+
allow_mixed=True,
1917+
require_iso8601=require_iso8601,
19151918
)
19161919
if result.dtype == object:
19171920
return result
@@ -1932,6 +1935,7 @@ def sequence_to_dt64ns(
19321935
*,
19331936
allow_object: bool = False,
19341937
allow_mixed: bool = False,
1938+
require_iso8601: bool = False,
19351939
):
19361940
"""
19371941
Parameters
@@ -1949,6 +1953,8 @@ def sequence_to_dt64ns(
19491953
data contains more than one timezone.
19501954
allow_mixed : bool, default False
19511955
Interpret integers as timestamps when datetime objects are also present.
1956+
require_iso8601 : bool, default False
1957+
Only consider ISO-8601 formats when parsing strings.
19521958
19531959
Returns
19541960
-------
@@ -2017,6 +2023,7 @@ def sequence_to_dt64ns(
20172023
yearfirst=yearfirst,
20182024
allow_object=allow_object,
20192025
allow_mixed=allow_mixed,
2026+
require_iso8601=require_iso8601,
20202027
)
20212028
if tz and inferred_tz:
20222029
# two timezones: convert to intended from base UTC repr
@@ -2083,8 +2090,8 @@ def objects_to_datetime64ns(
20832090
yearfirst,
20842091
utc=False,
20852092
errors="raise",
2086-
require_iso8601=False,
2087-
allow_object=False,
2093+
require_iso8601: bool = False,
2094+
allow_object: bool = False,
20882095
allow_mixed: bool = False,
20892096
):
20902097
"""

pandas/core/dtypes/cast.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -1519,40 +1519,25 @@ def maybe_infer_to_datetimelike(value: Union[np.ndarray, List]):
15191519
def try_datetime(v: np.ndarray) -> ArrayLike:
15201520
# Coerce to datetime64, datetime64tz, or in corner cases
15211521
# object[datetimes]
1522-
from pandas.core.arrays.datetimes import (
1523-
DatetimeArray,
1524-
objects_to_datetime64ns,
1525-
tz_to_dtype,
1526-
)
1522+
from pandas.core.arrays.datetimes import sequence_to_datetimes
15271523

15281524
try:
15291525
# GH#19671 we pass require_iso8601 to be relatively strict
15301526
# when parsing strings.
1531-
vals, tz = objects_to_datetime64ns(
1532-
v,
1533-
require_iso8601=True,
1534-
dayfirst=False,
1535-
yearfirst=False,
1536-
allow_object=True,
1537-
)
1527+
dta = sequence_to_datetimes(v, require_iso8601=True, allow_object=True)
15381528
except (ValueError, TypeError):
15391529
# e.g. <class 'numpy.timedelta64'> is not convertible to datetime
15401530
return v.reshape(shape)
15411531
else:
1542-
# we might have a sequence of the same-datetimes with tz's
1543-
# if so coerce to a DatetimeIndex; if they are not the same,
1544-
# then these stay as object dtype, xref GH#19671
1545-
1546-
if vals.dtype == object:
1532+
if dta.dtype == object or dta.tz is None:
1533+
# GH#19671 if we have mixed timezones we may have object-dtype
1534+
# here.
15471535
# This is reachable bc allow_object=True, means we cast things
15481536
# to mixed-tz datetime objects (mostly). Only 1 test
15491537
# relies on this behavior, see GH#40111
1550-
return vals.reshape(shape)
1551-
1552-
dta = DatetimeArray._simple_new(vals.view("M8[ns]"), dtype=tz_to_dtype(tz))
1553-
if dta.tz is None:
1554-
# TODO(EA2D): conditional reshape kludge unnecessary with 2D EAs
1555-
return dta._ndarray.reshape(shape)
1538+
# FIXME: conditional reshape is kludgy
1539+
return np.asarray(dta).reshape(shape)
1540+
# otherwise we have dt64tz
15561541
return dta
15571542

15581543
def try_timedelta(v: np.ndarray) -> np.ndarray:

0 commit comments

Comments
 (0)