You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Not sure if it is right, but my idea for the fix would be as below
checking if the first element has 'May' in the date string
if so then get the date format as usual. Suppose the date format we got is "%B-%d-%Y"
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.
Uh oh!
There was an error while loading. Please reload this page.
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
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:
Analysis:
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:
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
The text was updated successfully, but these errors were encountered: