Skip to content

Commit 007a761

Browse files
committed
added warnings when parse inconsistent with dayfirst arg
1 parent 04e9e0a commit 007a761

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

@@ -149,14 +150,28 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
149150
# date_string can't be converted to date, above format
150151
return None, None
151152

153+
swapped_day_and_month = False
152154
if 1 <= month <= MAX_DAYS_IN_MONTH and 1 <= day <= MAX_DAYS_IN_MONTH \
153155
and (month <= MAX_MONTH or day <= MAX_MONTH):
154156
if (month > MAX_MONTH or (day <= MAX_MONTH and dayfirst)) and can_swap:
155157
day, month = month, day
158+
swapped_day_and_month = True
156159
if PY_VERSION_HEX >= 0x03060100:
157160
# In Python <= 3.6.0 there is no range checking for invalid dates
158161
# in C api, thus we call faster C version for 3.6.1 or newer
162+
163+
if dayfirst and not swapped_day_and_month:
164+
warnings.warn(f"Parsing {date_string} MM/DD format.")
165+
elif not dayfirst and swapped_day_and_month:
166+
warnings.warn(f"Parsing {date_string} DD/MM format.")
167+
159168
return datetime_new(year, month, day, 0, 0, 0, 0, None), reso
169+
170+
if dayfirst and not swapped_day_and_month:
171+
warnings.warn(f"Parsing {date_string} MM/DD format.")
172+
elif not dayfirst and swapped_day_and_month:
173+
warnings.warn(f"Parsing {date_string} DD/MM format.")
174+
160175
return datetime(year, month, day, 0, 0, 0, 0, None), reso
161176

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

0 commit comments

Comments
 (0)