Skip to content

Commit 87c6433

Browse files
jbrockmendeljreback
authored andcommitted
REF: prelim for fixing array_to_datetime (#24000)
1 parent 7653a6b commit 87c6433

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

pandas/_libs/tslib.pyx

+1-4
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
463463
@cython.boundscheck(False)
464464
cpdef array_to_datetime(ndarray[object] values, str errors='raise',
465465
bint dayfirst=False, bint yearfirst=False,
466-
object format=None, object utc=None,
467-
bint require_iso8601=False):
466+
object utc=None, bint require_iso8601=False):
468467
"""
469468
Converts a 1D array of date-like values to a numpy array of either:
470469
1) datetime64[ns] data
@@ -488,8 +487,6 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
488487
dayfirst parsing behavior when encountering datetime strings
489488
yearfirst : bool, default False
490489
yearfirst parsing behavior when encountering datetime strings
491-
format : str, default None
492-
format of the string to parse
493490
utc : bool, default None
494491
indicator whether the dates should be UTC
495492
require_iso8601 : bool, default False

pandas/core/tools/datetimes.py

+28-26
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
231231
require_iso8601 = not infer_datetime_format
232232
format = None
233233

234+
tz_parsed = None
235+
result = None
234236
try:
235-
result = None
236-
237237
if format is not None:
238238
# shortcut formatting here
239239
if format == '%Y%m%d':
@@ -267,7 +267,8 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
267267
raise
268268
result = arg
269269

270-
if result is None and (format is None or infer_datetime_format):
270+
if result is None:
271+
assert format is None or infer_datetime_format
271272
result, tz_parsed = tslib.array_to_datetime(
272273
arg,
273274
errors=errors,
@@ -276,36 +277,37 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
276277
yearfirst=yearfirst,
277278
require_iso8601=require_iso8601
278279
)
279-
if tz_parsed is not None:
280-
if box:
281-
# We can take a shortcut since the datetime64 numpy array
282-
# is in UTC
283-
return DatetimeIndex._simple_new(result, name=name,
284-
tz=tz_parsed)
285-
else:
286-
# Convert the datetime64 numpy array to an numpy array
287-
# of datetime objects
288-
result = [Timestamp(ts, tz=tz_parsed).to_pydatetime()
289-
for ts in result]
290-
return np.array(result, dtype=object)
291-
292-
if box:
293-
# Ensure we return an Index in all cases where box=True
294-
if is_datetime64_dtype(result):
295-
return DatetimeIndex(result, tz=tz, name=name)
296-
elif is_object_dtype(result):
297-
# e.g. an Index of datetime objects
298-
from pandas import Index
299-
return Index(result, name=name)
300-
return result
301-
302280
except ValueError as e:
281+
# Fallback to try to convert datetime objects
303282
try:
304283
values, tz = conversion.datetime_to_datetime64(arg)
305284
return DatetimeIndex._simple_new(values, name=name, tz=tz)
306285
except (ValueError, TypeError):
307286
raise e
308287

288+
if tz_parsed is not None:
289+
if box:
290+
# We can take a shortcut since the datetime64 numpy array
291+
# is in UTC
292+
return DatetimeIndex._simple_new(result, name=name,
293+
tz=tz_parsed)
294+
else:
295+
# Convert the datetime64 numpy array to an numpy array
296+
# of datetime objects
297+
result = [Timestamp(ts, tz=tz_parsed).to_pydatetime()
298+
for ts in result]
299+
return np.array(result, dtype=object)
300+
301+
if box:
302+
# Ensure we return an Index in all cases where box=True
303+
if is_datetime64_dtype(result):
304+
return DatetimeIndex(result, tz=tz, name=name)
305+
elif is_object_dtype(result):
306+
# e.g. an Index of datetime objects
307+
from pandas import Index
308+
return Index(result, name=name)
309+
return result
310+
309311

310312
def _adjust_to_origin(arg, origin, unit):
311313
"""

0 commit comments

Comments
 (0)