Description
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
import datetime as dt
from pandas._libs.tslibs.parsing import guess_datetime_format
string = '2020-01-01 00:00:00 UTC+1'
pd.to_datetime([string], format=guess_datetime_format(string))
Issue Description
This returns:
In [20]: pd.to_datetime([string], format=guess_datetime_format(string))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [20], line 1
----> 1 pd.to_datetime([string], format=guess_datetime_format(string))
File ~/pandas-dev/pandas/core/tools/datetimes.py:1124, in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
1122 result = _convert_and_box_cache(argc, cache_array)
1123 else:
-> 1124 result = convert_listlike(argc, format)
1125 else:
1126 result = convert_listlike(np.array([arg]), format)[0]
File ~/pandas-dev/pandas/core/tools/datetimes.py:431, in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
428 format = None
430 if format is not None:
--> 431 res = _to_datetime_with_format(
432 arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
433 )
434 if res is not None:
435 return res
File ~/pandas-dev/pandas/core/tools/datetimes.py:539, in _to_datetime_with_format(arg, orig_arg, name, tz, fmt, exact, errors, infer_datetime_format)
536 return _box_as_indexlike(result, utc=utc, name=name)
538 # fallback
--> 539 res = _array_strptime_with_fallback(
540 arg, name, tz, fmt, exact, errors, infer_datetime_format
541 )
542 return res
File ~/pandas-dev/pandas/core/tools/datetimes.py:474, in _array_strptime_with_fallback(arg, name, tz, fmt, exact, errors, infer_datetime_format)
471 utc = tz == "utc"
473 try:
--> 474 result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
475 except OutOfBoundsDatetime:
476 if errors == "raise":
File ~/pandas-dev/pandas/_libs/tslibs/strptime.pyx:146, in pandas._libs.tslibs.strptime.array_strptime()
144 iresult[i] = NPY_NAT
145 continue
--> 146 raise ValueError(f"time data '{val}' does not match "
147 f"format '{fmt}' (match)")
148 if len(val) != found.end():
ValueError: time data '2020-01-01 00:00:00 UTC+1' does not match format '%Y-%m-%d %H:%M:%S UTC%z' (match)
Expected Behavior
I think guess_datetime_format(string)
should return None
here, because I don't think UTC+1
is valid in the standard library datetime
anyway (it should be UTC +01:00
), it's just dateutil.parser.parse
that guesses it
cc @mroeschke @jbrockmendel as the tests for this were added in #33133
Note that currently the parsing for this falls back to dateutil - guess_datetime_format
guesses the format, but then it can't parse the strings according to that format
I'd suggest:
- change that test to have
UTC-03:00
andUTC+03:00
- adding an extra test to check that
guess_datetime_format('2020-01-01 00:00:00 UTC+1')
returnsNone
Installed Versions
INSTALLED VERSIONS
commit : cfeed03
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.16.3-microsoft-standard-WSL2
Version : #1 SMP Fri Apr 2 22:23:49 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.6.0.dev0+251.gcfeed03306
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 59.8.0
pip : 22.2.2
Cython : 0.29.32
pytest : 7.1.3
hypothesis : 6.54.6
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : 8.5.0
pandas_datareader: 0.10.0
bs4 : 4.11.1
bottleneck : 1.3.5
brotli :
fastparquet : 0.8.3
fsspec : 2021.11.0
gcsfs : 2021.11.0
matplotlib : 3.6.0
numba : 0.56.2
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.8
pyarrow : 9.0.0
pyreadstat : 1.1.9
pyxlsb : 1.0.9
s3fs : 2021.11.0
scipy : 1.9.1
snappy :
sqlalchemy : 1.4.41
tables : 3.7.0
tabulate : 0.8.10
xarray : 2022.9.0
xlrd : 2.0.1
xlwt : 1.3.0
zstandard : 0.18.0
tzdata : None
None