Skip to content

BUG: parsing ISO8601 string with format= and timezone name fails #49748

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
3 tasks done
MarcoGorelli opened this issue Nov 17, 2022 · 1 comment · Fixed by #49747
Closed
3 tasks done

BUG: parsing ISO8601 string with format= and timezone name fails #49748

MarcoGorelli opened this issue Nov 17, 2022 · 1 comment · Fixed by #49747
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Nov 17, 2022

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

In pandas 1.5.1:

import pandas as pd
from pandas import *
import sys
import datetime as dt

print(dt.datetime.strptime('2020-01-01 00:00:00UTC', '%Y-%m-%d %H:%M:%S%Z'))  # works!
to_datetime('2020-01-01 00:00:00UTC', format='%Y-%m-%d %H:%M:%S%Z')  # crashes!

Issue Description

The ISO8601 fastpath in

https://github.com/pandas-dev/pandas/blob/6ec3d8f9dfd5dc89a2e9695ee4e01d8c280ebe2c/pandas/_libs/tslibs/src/datetime/np_datetime_strings.c

has no way to deal with '%Z' formatted-strings. They shouldn't get there in the first place.

Hence, '%Z' should be excluded from

def format_is_iso(f: str) -> bint:
"""
Does format match the iso8601 set that can be handled by the C parser?
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
but must be consistent. Leading 0s in dates and times are optional.
"""
iso_template = '%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}'.format
excluded_formats = ['%Y%m%d', '%Y%m', '%Y']
for date_sep in [' ', '/', '\\', '-', '.', '']:
for time_sep in [' ', 'T']:
for micro_or_tz in ['', '%z', '%Z', '.%f', '.%f%z', '.%f%Z']:
if (iso_template(date_sep=date_sep,
time_sep=time_sep,
micro_or_tz=micro_or_tz,
).startswith(f) and f not in excluded_formats):
return True
return False

Expected Behavior

In [1]: to_datetime('2020-01-01 00:00:00UTC', format='%Y-%m-%d %H:%M:%S%Z')
Out[1]: Timestamp('2020-01-01 00:00:00+0000', tz='UTC')

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.5.1
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.1
setuptools : 65.3.0
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.10.0
gcsfs : None
matplotlib : 3.6.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : 2022.10.0
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
None

@MarcoGorelli MarcoGorelli added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 17, 2022
@MarcoGorelli
Copy link
Member Author

@MarcoGorelli MarcoGorelli changed the title BUG: parsing ISO8601 string with format= and UTC fails BUG: parsing ISO8601 string with format= and timezone name fails Nov 17, 2022
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

Successfully merging a pull request may close this issue.

1 participant