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.

  • [ ] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

>>> import pandas as pd
>>> def line_fixer(line):     
...     return [1, 2, 3, 4, 5]
... 
>>> df = pd.read_csv('test.csv', engine='python', on_bad_lines=line_fixer)
<stdin>:1: ParserWarning: Length of header or names does not match length of data. This leads to a loss of data with index_col=False.
>>> df = pd.read_csv('test.csv', engine='python', on_bad_lines=line_fixer, index_col=0)
>>>

Issue Description

test.csv, with extra column ("E") in row 3

id,field_1,field_2
101,A,B
102,C,D,E
103,F,G

Callable line_fixer returns a list with 5 elements, which is more elements than expected.

Documentation for the read_csv() on_bad_lines callable states:

If the function returns a new list of strings with more elements than expected, a ParserWarning will be emitted while dropping extra elements.

This behavior is correctly seen when index_col=None (the default), but not when index_col is set.

Expected Behavior

A ParserWarning should be raised regardless of the index_col parameter. In either case, data (elements 4 and 5, in this example) are being lost, but this is done silently when index_col is set.

Installed Versions

INSTALLED VERSIONS ------------------ commit : c888af6d0bb674932007623c0867e1fbd4bdc2c6 python : 3.10.4 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.26100 machine : AMD64 processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : English_United States.1252 pandas : 2.3.1 numpy : 2.2.5 pytz : 2025.2 dateutil : 2.9.0.post0 pip : 25.1.1 Cython : None sphinx : None IPython : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : None lxml.etree : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None psycopg2 : None pymysql : None pyarrow : 20.0.0 pyreadstat : None pytest : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlsxwriter : 3.2.3 zstandard : None tzdata : 2025.2 qtpy : None pyqt5 : None