BUG: to_datetime fails to infer timeformat depending on hour of day · Issue #57452 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd from pandas._libs.tslibs.parsing import guess_datetime_format import dateutil import time

def demo(): print(pd.version) print(guess_datetime_format("2023-11-09T19:23:46Z")) print(guess_datetime_format("2023-11-09T20:23:46Z")) print(dateutil.version) print(dateutil.parser.parse("2023-11-09T19:23:46Z")) print(dateutil.parser.parse("2023-11-09T20:23:46Z")) print(dateutil.parser.DEFAULTPARSER._parse("2023-11-09T19:23:46Z")) print(dateutil.parser.DEFAULTPARSER._parse("2023-11-09T20:23:46Z")) print(time.tzname)

2.2.0 %Y-%m-%dT%H:%M:%S%z None 2.8.2 2023-11-09 19:23:46+00:00 2023-11-09 20:23:46+00:00 (_result(year=2023, month=11, day=9, hour=19, minute=23, second=46, microsecond=0, tzname='UTC', tzoffset=0), None) (_result(year=2023, month=11, day=9, hour=20, minute=23, second=46, microsecond=0, tzname='UTC', tzoffset=0), None) ('CET', 'CEST')

3.0.0.dev0+355.g92a52e2315 %Y-%m-%dT%H:%M:%S%z None 2.8.2 2023-11-09 19:23:46+00:00 2023-11-09 20:23:46+00:00 (_result(year=2023, month=11, day=9, hour=19, minute=23, second=46, microsecond=0, tzname='UTC', tzoffset=0), None) (_result(year=2023, month=11, day=9, hour=20, minute=23, second=46, microsecond=0, tzname='UTC', tzoffset=0), None) ('CET', 'CEST')

Issue Description

If the first data value is 2023-11-09T19:23:46Z to_datetime infers format.
If the first data value is 2023-11-09T20:23:46Z inference fails with:

UserWarning: Could not infer format, so each element will be parsed individually, falling back to dateutil. To ensure parsing is consistent and as-expected, please specify a format.

Expected Behavior

Format should be inferred.

Installed Versions

INSTALLED VERSIONS

commit : 92a52e2
python : 3.12.1.final.0
python-bits : 64
OS : Darwin
OS-release : 23.3.0
Version : Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:10 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6031
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 3.0.0.dev0+355.g92a52e2315
numpy : 2.0.0.dev0+git20240210.2946577
pytz : 2024.1
dateutil : 2.8.2
setuptools : None
pip : 23.3.1
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
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