BUG: pandas.read_fwf does not throw warning or error when column spec and name list lengths differ #40830
Closed
2 tasks done
Labels
Milestone
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
Code Sample, a copy-pastable example
Problem description
When passing both the "names" and "colspecs" parameters, pandas.read_fwf fails to raise a warning/error when the length of the lists passed to these parameters does not match. In the given example, colspec_good has one tuple for every header name; colspec_bad has an extra 23rd tuple with no matching name. When the given file contains one row, a ParserError is raised with colspec_bad; but if given more than one row, no such warning/error is raised.
I propose that pandas.read_fwf should raise a warning/error when the length of the list passed to "names" doesn't match the length of the list passed to "colspecs" (when both parameters are used). Practically speaking, this tells the developer that they need to fix their header/colspecs. From a healthcare industry perspective, this has many nontrivial applications - e.g., the ingestion of standard, fixed-width CMS Medicare Advantage and CCLF (claim and claim line feed) files.
Expected Output
ParserError: Expected 22 fields in line 1, saw 23
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : f2c8480
python : 3.7.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.2.3
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.22
pytest : 6.2.3
hypothesis : None
sphinx : 3.5.3
blosc : None
feather : None
xlsxwriter : 1.3.8
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.7
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : 1.4.5
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1
The text was updated successfully, but these errors were encountered: