Skip to content

Commit

Permalink
Backport PR #45522: REGR: read_fwf interpreting infer as list of cols…
Browse files Browse the repository at this point in the history
…pecs when checking names length (#45530)

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Jan 21, 2022
1 parent ff4c6d1 commit 72117ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ def read_fwf(
# Ensure length of `colspecs` matches length of `names`
names = kwds.get("names")
if names is not None:
if len(names) != len(colspecs):
if len(names) != len(colspecs) and colspecs != "infer":
# need to check len(index_col) as it might contain
# unnamed indices, in which case it's name is not required
len_index = 0
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/parser/test_read_fwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,13 @@ def test_skiprows_passing_as_positional_deprecated():
result = read_fwf(StringIO(data), [(0, 2)])
expected = DataFrame({"0": [1, 2]})
tm.assert_frame_equal(result, expected)


def test_names_and_infer_colspecs():
# GH#45337
data = """X Y Z
959.0 345 22.2
"""
result = read_fwf(StringIO(data), skiprows=1, usecols=[0, 2], names=["a", "b"])
expected = DataFrame({"a": [959.0], "b": 22.2})
tm.assert_frame_equal(result, expected)

0 comments on commit 72117ae

Please sign in to comment.