BUG: to_datetime with specific format are incorrectly matching

This issue has been created 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-09-29
Star Count 35374
Watcher Count 1122
Fork Count 15034
Issue Count 3579

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Canvas turns white when adding point for curved line 6 2022-08-11 2022-09-08
requremennt out of date 2 2022-03-22 2022-09-15
Does window.getScreenDetails() always resolve with the same object? 4 2022-01-08 2022-09-11
How to make the output of neat to be only binary numbers? 1 2020-05-05 2022-09-08
CodeMirror box on TextBox page is kinda broken 0 2022-08-15 2022-09-17
One typoerror for speedup description in README 0 2022-07-22 2022-09-21
[V1.4.0] gazelle plugin crash 10 2022-07-22 2022-09-21
can we support IsolatedModules by default? 5 2022-05-02 2022-09-19
Intial Connection to a new IP extremly slow even tough download & upload speed is fast 6 2022-04-28 2022-09-02
Multi-Contract Interaction - Error: Transaction reverted and Hardhat couldn't infer the reason. 6 2022-05-07 2022-09-18
choose your own download storage location 0 2022-05-08 2022-09-02
m3u8 video freezes, sound plays 3 2022-05-06 2022-09-02
DH key exchange implementation is slow (pow) 4 2022-04-21 2022-09-18
Another crash on startup with master 6 2021-10-12 2022-02-10
BUG!!! NOT COMPILE! 1 2021-07-14 2021-12-29
Fedora Linux installing and Pls readme update 0 2021-08-19 2022-08-01
[improvement] Move handles on small selections (rather than hiding them) 0 2021-11-20 2022-07-26
Message parser on the IMessage interface. 1 2021-01-12 2022-09-28
HMAC section of README outdated 3 2021-06-02 2022-09-23
Notifications on TLS cert expiry 1 2021-04-12 2022-08-17
questions about quality of SMPLify-X fitting error 13.1mm 0 2021-07-23 2022-09-07
Unable to update prosemirror-transform to v1.4.2 1 2022-05-05 2022-09-11
Useless escaping quotes 1 2021-06-16 2022-09-11
Create entity if not found 2 2022-09-18 2022-09-15
generate-entities - postgresql type bigint is referred as string in typescript 1 2022-09-18 2022-09-15
Node sent the MYST to a unknown address 0 2022-06-22 2022-07-31
Document meaning of alpha/beta functions 15 2020-05-01 2022-09-16
Loadable Bundles are required with OBS Studio 28.0 and beyond 6 2022-08-20 2022-09-02
MSBUILD : error MSB1001: Unknown switch. 0 2021-06-30 2022-09-08
HW04 Questions 1 2021-11-17 2021-12-26
[FIRRTL] Canonicalize integer interval tests 0 2022-04-21 2022-09-06
Feature Request: Selecting Plugins? 3 2022-07-23 2022-09-18
minecraft won't open at all (minecraft) 5 2021-06-10 2022-09-28
[Bug] IOS 15.2 ConnectFailure (Network is unreachable) SOAP Webservices 3 2022-01-13 2022-08-27
Widget: Item from props cannot be resolved for oh-aggregate-series 0 2022-02-02 2022-08-17
Uniformisation Formulaires user / liste blanche mandataires individuels 0 2021-12-01 2022-07-15
Remove all uses of "differential" from the docs 0 2021-08-18 2022-08-18
QScrollArea prop issue 2 2021-11-12 2022-09-10
Add Support Symbol Information 2 2021-07-09 2022-09-10
Just use issues instead of RFC process 8 2021-06-24 2022-09-28
Improve JSON support in Esqueleto.Postgresql 0 2021-09-10 2022-09-23
Add pallet methods for Polkadot & Kusama PLOs 0 2021-03-15 2022-09-17
Decoded tx is not backward compatible 1 2021-02-23 2022-09-17
Process crashes with DeadObjectException 1 2021-08-30 2022-08-11
Terminal Issues 7 2019-02-27 2022-01-15
Source control panel - sync changes button when syncing is not in loading state 5 2022-01-14 2022-09-24
`stack new` repeats missing parameter warning 1 2022-07-18 2022-09-09
Transient "file returned by server too large" error 0 2022-07-02 2022-09-09
FrescoImage cannot load animated gif image 1 2021-02-08 2022-09-21
I just want to run simple code and this happened 3 2022-02-09 2022-08-28