Skip to content

Commit 78e6bc7

Browse files
BUG: Edited documentation and improved tests pandas-dev#46071
1 parent 2ab558a commit 78e6bc7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pandas/core/tools/datetimes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,10 @@ def to_datetime(
796796
If :const:`True` and no `format` is given, attempt to infer the format
797797
of the datetime strings based on the first non-NaN element,
798798
and if it can be inferred, switch to a faster method of parsing them.
799-
In some cases this can increase the parsing speed by ~5-10x.
799+
In some cases this can increase the parsing speed by ~5-10x. If subsequent
800+
datetime strings do not follow the inferred format, parsing will fall
801+
back to the slower method of determining the format for each
802+
string individually.
800803
origin : scalar, default 'unix'
801804
Define the reference date. The numeric values would be parsed as number
802805
of units (defined by `unit`) since this reference date.

pandas/tests/arrays/test_datetimes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"""
44
import operator
55

6+
from dateutil.parser._parser import ParserError
67
import numpy as np
78
import pytest
89

910
from pandas._libs.tslibs import tz_compare
1011
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
12+
from pandas.errors import OutOfBoundsDatetime
1113

1214
from pandas.core.dtypes.dtypes import DatetimeTZDtype
1315

@@ -657,3 +659,22 @@ def test_coerce_fallback(self, error):
657659
assert result[1] is not pd.NaT
658660

659661
tm.assert_series_equal(expected, result)
662+
663+
expected2 = pd.Series([pd.Timestamp("2000-01-01 00:00:00"), pd.NaT])
664+
665+
es1 = pd.Series(["1/1/2000", "7/12/1200"])
666+
es2 = pd.Series(["1/1/2000", "Hello"])
667+
668+
if error == "coerce":
669+
eres1 = pd.to_datetime(es1, errors=error, infer_datetime_format=True)
670+
eres2 = pd.to_datetime(es2, errors=error, infer_datetime_format=True)
671+
tm.assert_series_equal(expected2, eres1)
672+
tm.assert_series_equal(expected2, eres2)
673+
else:
674+
with pytest.raises(
675+
OutOfBoundsDatetime, match="Out of bounds nanosecond timestamp"
676+
):
677+
pd.to_datetime(es1, errors=error, infer_datetime_format=True)
678+
679+
with pytest.raises(ParserError, match="Unknown string format: Hello"):
680+
pd.to_datetime(es2, errors=error, infer_datetime_format=True)

0 commit comments

Comments
 (0)