Skip to content

Commit 2ab558a

Browse files
BUG: Added test and release note and removed line shifts pandas-dev#46071
1 parent 7c69de8 commit 2ab558a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ Datetimelike
827827
- Bug in :meth:`DatetimeIndex.resolution` incorrectly returning "day" instead of "nanosecond" for nanosecond-resolution indexes (:issue:`46903`)
828828
- Bug in :class:`Timestamp` with an integer or float value and ``unit="Y"`` or ``unit="M"`` giving slightly-wrong results (:issue:`47266`)
829829
- Bug in :class:`.DatetimeArray` construction when passed another :class:`.DatetimeArray` and ``freq=None`` incorrectly inferring the freq from the given array (:issue:`47296`)
830-
-
830+
- Bug in :func:`to_datetime` where ``infer_datetime_format`` fallback would not run if ``errors=coerce`` (:issue:`46071`)
831831

832832
Timedelta
833833
^^^^^^^^^

pandas/core/tools/datetimes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ def _maybe_cache(
215215
cache_array : Series
216216
Cache of converted, unique dates. Can be empty
217217
"""
218-
219218
from pandas import Series
220219

221220
cache_array = Series(dtype=object)
@@ -392,6 +391,7 @@ def _convert_listlike_datetimes(
392391
raise TypeError(
393392
"arg must be a string, datetime, list, tuple, 1-d array, or Series"
394393
)
394+
395395
# warn if passing timedelta64, raise for PeriodDtype
396396
# NB: this must come after unit transformation
397397
orig_arg = arg
@@ -411,6 +411,7 @@ def _convert_listlike_datetimes(
411411

412412
if infer_datetime_format and format is None:
413413
format = _guess_datetime_format_for_array(arg, dayfirst=dayfirst)
414+
414415
if format is not None:
415416
# There is a special fast-path for iso8601 formatted
416417
# datetime strings, so in those cases don't use the inferred
@@ -427,6 +428,7 @@ def _convert_listlike_datetimes(
427428
)
428429
if res is not None:
429430
return res
431+
430432
assert format is None or infer_datetime_format
431433
utc = tz == "utc"
432434
result, tz_parsed = objects_to_datetime64ns(
@@ -438,6 +440,7 @@ def _convert_listlike_datetimes(
438440
require_iso8601=require_iso8601,
439441
allow_object=True,
440442
)
443+
441444
if tz_parsed is not None:
442445
# We can take a shortcut since the datetime64 numpy array
443446
# is in UTC
@@ -492,7 +495,7 @@ def _array_strptime_with_fallback(
492495
else:
493496
if "%Z" in fmt or "%z" in fmt:
494497
return _return_parsed_timezone_results(result, timezones, tz, name)
495-
# GH#46071
498+
496499
if infer_datetime_format and np.isnan(result).any():
497500
return None
498501

@@ -513,6 +516,7 @@ def _to_datetime_with_format(
513516
Try parsing with the given format, returning None on failure.
514517
"""
515518
result = None
519+
516520
# shortcut formatting here
517521
if fmt == "%Y%m%d":
518522
# pass orig_arg as float-dtype may have been converted to
@@ -1028,7 +1032,6 @@ def to_datetime(
10281032
'2020-01-01 18:00:00+00:00', '2020-01-01 19:00:00+00:00'],
10291033
dtype='datetime64[ns, UTC]', freq=None)
10301034
"""
1031-
10321035
if arg is None:
10331036
return None
10341037

pandas/tests/arrays/test_datetimes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,21 @@ def test_tz_localize_t2d(self):
639639

640640
roundtrip = expected.tz_localize("US/Pacific")
641641
tm.assert_datetime_array_equal(roundtrip, dta)
642+
643+
@pytest.mark.parametrize(
644+
"error",
645+
["coerce", "raise"],
646+
)
647+
def test_coerce_fallback(self, error):
648+
# GH#46071
649+
s = pd.Series(["6/30/2025", "1 27 2024"])
650+
expected = pd.Series(
651+
[pd.Timestamp("2025-06-30 00:00:00"), pd.Timestamp("2024-01-27 00:00:00")]
652+
)
653+
654+
result = pd.to_datetime(s, errors=error, infer_datetime_format=True)
655+
656+
if error == "coerce":
657+
assert result[1] is not pd.NaT
658+
659+
tm.assert_series_equal(expected, result)

0 commit comments

Comments
 (0)