@@ -1088,6 +1088,7 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
1088
1088
# rebuild string, capturing any inferred padding
1089
1089
dt_str = ' ' .join(tokens)
1090
1090
if parsed_datetime.strftime(guessed_format) == dt_str:
1091
+ _maybe_warn_about_dayfirst(guessed_format, dayfirst)
1091
1092
return guessed_format
1092
1093
else :
1093
1094
return None
@@ -1106,6 +1107,26 @@ cdef str _fill_token(token: str, padding: int):
1106
1107
token_filled = f' {seconds}.{nanoseconds}'
1107
1108
return token_filled
1108
1109
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
+
1109
1130
@ cython.wraparound (False )
1110
1131
@ cython.boundscheck (False )
1111
1132
cdef inline object convert_to_unicode(object item, bint keep_trivial_numbers):
0 commit comments