BUG: to_datetime with specific format are incorrectly matching

This issue has been tracked since 2022-09-22.

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

ser = pd.Series(["2022\\12\\25"])

pd.to_datetime(ser, format="%Y-%m-%d") # this should fail, because format does not match

Issue Description

There is an inconsisten behaviour between datetime library and pandas.to_datetime function when dealing with specific format.
When using slashes, it matches format though it shouldn't

Expected Behavior

It should throw an Exception because the format does not match the values in the Series

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.5.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 :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.3.0
pip : 22.2.2
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 : 8.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

MarcoGorelli wrote this answer on 2022-09-22

thanks for the report - looks like a duplicate of #12649 ?

dadiego91 wrote this answer on 2022-09-22

I think it can be related, but in that ticket they talk about the nuance of matching from the start of the string or the whole, but this is about a completely mismatch of characters, because "-" has nothing to do with "/"

MarcoGorelli wrote this answer on 2022-09-22

I think it's the same, it's due to the ISO8601 fastpath

(Pdb) p format_is_iso(format)
True
MarcoGorelli wrote this answer on 2022-09-22

closing for now, but thanks for the report, I'll looking into addressing this as I also find it to be surprising

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-10-04
Star Count 35430
Watcher Count 1120
Fork Count 15089
Issue Count 3589

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date