Skip to content

REGR: read_fwf interpreting infer as list of colspecs when checking names length #45522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)