Pandas version checks

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

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

  • [X] 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

https://github.com/pandas-dev/pandas/blob/634196a078b1d38dba4c6bd62a8f6a0af40fa1d9/pandas/_libs/tslibs/parsing.pyx#L886-L903

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 : 91111fd99898d9dcaa6bf6bedb662db4108da6e6 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

Comment From: MarcoGorelli

This was introduced with https://github.com/pandas-dev/pandas/pull/42068/files