Skip to content
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

BUG: pd.to_datetime fails to identify actual date format #58747

Closed
3 tasks done
RedHeadphone opened this issue May 17, 2024 · 4 comments
Closed
3 tasks done

BUG: pd.to_datetime fails to identify actual date format #58747

RedHeadphone opened this issue May 17, 2024 · 4 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@RedHeadphone
Copy link

RedHeadphone commented May 17, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

data = [
  "May-16-2024",
  "May-17-2024",
  "May-18-2024",
  "May-20-2024",
  "May-24-2024",
  "May-27-2024",
  "May-28-2024",
  "Jun-05-2024"
]

ser = pd.Series(data)
ser = pd.to_datetime(ser)

print(ser)

Issue Description

Problem:
When converting a list of date strings to datetime using pd.to_datetime, it raises an error if the month format is inconsistent (i.e., some months are in full form and others are abbreviated). This issue arises even when the data is not mixed but misinterpreted by pandas due to the nature of certain month abbreviations.

Error:

ValueError: time data "Jun-05-2024" doesn't match format "%B-%d-%Y", at position 7. You might want to try:
    - passing `format` if your strings have a consistent format;
    - passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
    - passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.

Analysis:

  • The input format "%B-%d-%Y" (full month name) is inferred from the first element "May-16-2024".
  • This format works for "May-16-2024" through "May-28-2024" because "May" has the same abbreviation as its full form.
  • The problem arises when it encounters "Jun-05-2024", where "Jun" is the abbreviated form and does not match the inferred full month name format.

Current Workaround:
Specifying the format as format='mixed' or format='%b-%d-%Y' solves the issue, but this should ideally be handled by pandas automatically.

Expected Behavior

pd.to_datetime should correctly parse the dates without raising an error.

Output should be:

0   2024-05-16
1   2024-05-17
2   2024-05-18
3   2024-05-20
4   2024-05-24
5   2024-05-27
6   2024-05-28
7   2024-06-05
dtype: datetime64[ns]

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.14.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-1020-gcp
Version : #20~22.04.1-Ubuntu SMP Wed May 1 02:03:24 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.5.1.post0
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@RedHeadphone RedHeadphone added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 17, 2024
@Aloqeely
Copy link
Member

Thanks for the report! This is a duplicate of #58328

But, your analysis is correct and if you have an idea for a fix feel free to submit a PR!

@RedHeadphone
Copy link
Author

RedHeadphone commented May 17, 2024

Not sure if it is right, but my idea for the fix would be as below

  1. checking if the first element has 'May' in the date string
  2. if so then get the date format as usual. Suppose the date format we got is "%B-%d-%Y"
  3. then try converting all dates with "%B-%d-%Y" and "%b-%d-%Y" (replace capital B with small b) . If one of them doesn't work, continue with the other format.

@Aloqeely what do you think of this approach?

Edit:
step 3 could be like below

format = get_format(data[0])  # "%B-%d-%Y"
formats_to_try = [format, format.replace('%B', '%b')]

for fmt in formats_to_try:
    try:
        return pd.to_datetime(*same_args, format=fmt)
    except ValueError:
        continue

raise ValueError("Mixed date formats")

@Aloqeely
Copy link
Member

Aloqeely commented May 17, 2024

That would work, but 'May' is not the only month name where this happens, for example 'Mai' in French has the same problem.

Could you please discuss in the original issue and get input from pandas devs?

@RedHeadphone
Copy link
Author

Sure closing this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants