Skip to content

Commit fbca843

Browse files
author
MarcoGorelli
committed
Revert "Inconsistent date parsing of to_datetime (#42908)"
This reverts commit 36e4165.
1 parent 6598797 commit fbca843

File tree

4 files changed

+8
-35
lines changed

4 files changed

+8
-35
lines changed

doc/source/user_guide/timeseries.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ you can pass the ``dayfirst`` flag:
214214
215215
.. warning::
216216

217-
You see in the above example that ``dayfirst`` isn't strict. If a date
217+
You see in the above example that ``dayfirst`` isn't strict, so if a date
218218
can't be parsed with the day being first it will be parsed as if
219219
``dayfirst`` were ``False`` and a warning will also be raised.
220220

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Notable bug fixes
249249

250250
These are bug fixes that might have notable behavior changes.
251251

252-
.. _whatsnew_140.notable_bug_fixes.inconsistent_date_string_parsing:
252+
.. _whatsnew_140.notable_bug_fixes.notable_bug_fix1:
253253

254254
Inconsistent date string parsing
255255
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pandas/_libs/tslibs/parsing.pyx

-24
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ class DateParseError(ValueError):
8585
_DEFAULT_DATETIME = datetime(1, 1, 1).replace(hour=0, minute=0,
8686
second=0, microsecond=0)
8787

88-
PARSING_WARNING_MSG = (
89-
"Parsing dates in {format} format when dayfirst={dayfirst} was specified. "
90-
"This may lead to inconsistently parsed dates! Specify a format "
91-
"to ensure consistent parsing."
92-
)
93-
9488
cdef:
9589
set _not_datelike_strings = {"a", "A", "m", "M", "p", "P", "t", "T"}
9690

@@ -203,28 +197,10 @@ cdef object _parse_delimited_date(str date_string, bint dayfirst):
203197
# date_string can't be converted to date, above format
204198
return None, None
205199

206-
swapped_day_and_month = False
207200
if 1 <= month <= MAX_DAYS_IN_MONTH and 1 <= day <= MAX_DAYS_IN_MONTH \
208201
and (month <= MAX_MONTH or day <= MAX_MONTH):
209202
if (month > MAX_MONTH or (day <= MAX_MONTH and dayfirst)) and can_swap:
210203
day, month = month, day
211-
swapped_day_and_month = True
212-
if dayfirst and not swapped_day_and_month:
213-
warnings.warn(
214-
PARSING_WARNING_MSG.format(
215-
format="MM/DD/YYYY",
216-
dayfirst="True",
217-
),
218-
stacklevel=find_stack_level(),
219-
)
220-
elif not dayfirst and swapped_day_and_month:
221-
warnings.warn(
222-
PARSING_WARNING_MSG.format(
223-
format="DD/MM/YYYY",
224-
dayfirst="False (the default)",
225-
),
226-
stacklevel=find_stack_level(),
227-
)
228204
# In Python <= 3.6.0 there is no range checking for invalid dates
229205
# in C api, thus we call faster C version for 3.6.1 or newer
230206
return datetime_new(year, month, day, 0, 0, 0, 0, None), reso

pandas/tests/io/parser/test_parse_dates.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
timezone,
1111
)
1212
from io import StringIO
13-
import warnings
1413

1514
from dateutil.parser import parse as du_parse
1615
from hypothesis import given
@@ -1665,16 +1664,16 @@ def test_invalid_parse_delimited_date(all_parsers, date_string):
16651664
"date_string,dayfirst,expected",
16661665
[
16671666
# %d/%m/%Y; month > 12 thus replacement
1667+
("13/02/2019", False, datetime(2019, 2, 13)),
16681668
("13/02/2019", True, datetime(2019, 2, 13)),
16691669
# %m/%d/%Y; day > 12 thus there will be no replacement
16701670
("02/13/2019", False, datetime(2019, 2, 13)),
1671+
("02/13/2019", True, datetime(2019, 2, 13)),
16711672
# %d/%m/%Y; dayfirst==True thus replacement
16721673
("04/02/2019", True, datetime(2019, 2, 4)),
16731674
],
16741675
)
1675-
def test_parse_delimited_date_swap_no_warning(
1676-
all_parsers, date_string, dayfirst, expected
1677-
):
1676+
def test_parse_delimited_date_swap(all_parsers, date_string, dayfirst, expected):
16781677
parser = all_parsers
16791678
expected = DataFrame({0: [expected]}, dtype="datetime64[ns]")
16801679
result = parser.read_csv(
@@ -1751,11 +1750,9 @@ def test_hypothesis_delimited_date(
17511750
)
17521751
date_string = test_datetime.strftime(date_format.replace(" ", delimiter))
17531752

1754-
with warnings.catch_warnings():
1755-
warnings.filterwarnings("ignore", category=UserWarning)
1756-
except_out_dateutil, result = _helper_hypothesis_delimited_date(
1757-
parse_datetime_string, date_string, dayfirst=dayfirst
1758-
)
1753+
except_out_dateutil, result = _helper_hypothesis_delimited_date(
1754+
parse_datetime_string, date_string, dayfirst=dayfirst
1755+
)
17591756
except_in_dateutil, expected = _helper_hypothesis_delimited_date(
17601757
du_parse,
17611758
date_string,

0 commit comments

Comments
 (0)