Skip to content

Commit ab78002

Browse files
author
MarcoGorelli
committed
🚨 add warning about dayfirst
1 parent 1104a92 commit ab78002

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pandas/_libs/tslibs/parsing.pyx

+21
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,7 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
10881088
# rebuild string, capturing any inferred padding
10891089
dt_str = ''.join(tokens)
10901090
if parsed_datetime.strftime(guessed_format) == dt_str:
1091+
_maybe_warn_about_dayfirst(guessed_format, dayfirst)
10911092
return guessed_format
10921093
else:
10931094
return None
@@ -1106,6 +1107,26 @@ cdef str _fill_token(token: str, padding: int):
11061107
token_filled = f'{seconds}.{nanoseconds}'
11071108
return token_filled
11081109

1110+
cdef void _maybe_warn_about_dayfirst(format: str, bint dayfirst):
1111+
"""Warn if guessed datetime format doesn't respect dayfirst argument."""
1112+
cdef:
1113+
int day_index = format.find('%d')
1114+
int month_index = format.find('%m')
1115+
1116+
if (day_index != -1) and (month_index != -1):
1117+
if (day_index > month_index) and dayfirst:
1118+
warnings.warn(
1119+
f"Parsing dates in {format} format when dayfirst=True was specified. "
1120+
f"Pass `dayfirst=False` or specify a format to silence this warning.",
1121+
stacklevel=find_stack_level(),
1122+
)
1123+
if (day_index < month_index) and not dayfirst:
1124+
warnings.warn(
1125+
f"Parsing dates in {format} format when dayfirst=False was specified. "
1126+
f"Pass `dayfirst=True` or specify a format to silence this warning.",
1127+
stacklevel=find_stack_level(),
1128+
)
1129+
11091130
@cython.wraparound(False)
11101131
@cython.boundscheck(False)
11111132
cdef inline object convert_to_unicode(object item, bint keep_trivial_numbers):

0 commit comments

Comments
 (0)