8000 WIP BUG: Inconsistent date parsing of to_datetime by arw2019 · Pull Request #35428 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

WIP BUG: Inconsistent date parsing of to_datetime #35428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added warnings when parse inconsistent with dayfirst arg
  • Loading branch information
arw2019 committed Jul 27, 2020
commit 007a7619b1125c5a65eb76fce100f4020de7ea73
15 changes: 15 additions & 0 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Parsing functions for datetime and datetime-like strings.
"""
import re
import time
import warnings

from libc.string cimport strchr

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

swapped_day_and_month = False
if 1 <= month <= MAX_DAYS_IN_MONTH and 1 <= day <= MAX_DAYS_IN_MONTH \
and (month <= MAX_MONTH or day <= MAX_MONTH):
if (month > MAX_MONTH or (day <= MAX_MONTH and dayfirst)) and can_swap:
day, month = month, day
swapped_day_and_month = True
if PY_VERSION_HEX >= 0x03060100:
# In Python <= 3.6.0 there is no range checking for invalid dates
# in C api, thus we call faster C version for 3.6.1 or newer

if dayfirst and not swapped_day_and_month:
warnings.warn(f"Parsing {date_string} MM/DD format.")
elif not dayfirst and swapped_day_and_month:
warnings.warn(f"Parsing {date_string} DD/MM format.")

return datetime_new(year, month, day, 0, 0, 0, 0, None), reso

if dayfirst and not swapped_day_and_month:
warnings.warn(f"Parsing {date_string} MM/DD format.")
elif not dayfirst and swapped_day_and_month:
warnings.warn(f"Parsing {date_string} DD/MM format.")

return datetime(year, month, day, 0, 0, 0, 0, None), reso

raise DateParseError(f"Invalid date specified ({month}/{day})")
Expand Down
0