Skip to content

Commit 72117ae

Browse files
Backport PR pandas-dev#45522: REGR: read_fwf interpreting infer as list of colspecs when checking names length (pandas-dev#45530)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent ff4c6d1 commit 72117ae

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pandas/io/parsers/readers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def read_fwf(
856856
# Ensure length of `colspecs` matches length of `names`
857857
names = kwds.get("names")
858858
if names is not None:
859-
if len(names) != len(colspecs):
859+
if len(names) != len(colspecs) and colspecs != "infer":
860860
# need to check len(index_col) as it might contain
861861
# unnamed indices, in which case it's name is not required
862862
len_index = 0

pandas/tests/io/parser/test_read_fwf.py

+10
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,13 @@ def test_skiprows_passing_as_positional_deprecated():
920920
result = read_fwf(StringIO(data), [(0, 2)])
921921
expected = DataFrame({"0": [1, 2]})
922922
tm.assert_frame_equal(result, expected)
923+
924+
925+
def test_names_and_infer_colspecs():
926+
# GH#45337
927+
data = """X Y Z
928+
959.0 345 22.2
929+
"""
930+
result = read_fwf(StringIO(data), skiprows=1, usecols=[0, 2], names=["a", "b"])
931+
expected = DataFrame({"a": [959.0], "b": 22.2})
932+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)