Skip to content

Commit 8381574

Browse files
arw2019MarcoGorelli
authored andcommitted
added warnings when parse inconsistent with dayfirst arg
1 parent eaee348 commit 8381574

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

pandas/_libs/tslibs/parsing.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Parsing functions for datetime and datetime-like strings.
33
"""
44
import re
55
import time
6+
import warnings
67

78
from libc.string cimport strchr
89

@@ -168,14 +169,28 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
168169
# date_string can't be converted to date, above format
169170
return None, None
170171

172+
swapped_day_and_month = False
171173
if 1 <= month <= MAX_DAYS_IN_MONTH and 1 <= day <= MAX_DAYS_IN_MONTH \
172174
and (month <= MAX_MONTH or day <= MAX_MONTH):
173175
if (month > MAX_MONTH or (day <= MAX_MONTH and dayfirst)) and can_swap:
174176
day, month = month, day
177+
swapped_day_and_month = True
175178
if PY_VERSION_HEX >= 0x03060100:
176179
# In Python <= 3.6.0 there is no range checking for invalid dates
177180
# in C api, thus we call faster C version for 3.6.1 or newer
181+
182+
if dayfirst and not swapped_day_and_month:
183+
warnings.warn(f"Parsing {date_string} MM/DD format.")
184+
elif not dayfirst and swapped_day_and_month:
185+
warnings.warn(f"Parsing {date_string} DD/MM format.")
186+
178187
return datetime_new(year, month, day, 0, 0, 0, 0, None), reso
188+
189+
if dayfirst and not swapped_day_and_month:
190+
warnings.warn(f"Parsing {date_string} MM/DD format.")
191+
elif not dayfirst and swapped_day_and_month:
192+
warnings.warn(f"Parsing {date_string} DD/MM format.")
193+
179194
return datetime(year, month, day, 0, 0, 0, 0, None), reso
180195

181196
raise DateParseError(f"Invalid date specified ({month}/{day})")

0 commit comments

Comments
 (0)