Skip to content

REGR: Avoid error when defining non existing usecols with python engine #41244

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
May 12, 2021
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
15 changes: 10 additions & 5 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ def _exclude_implicit_index(self, alldata):
# error: Cannot determine type of 'index_col'
offset = len(self.index_col) # type: ignore[has-type]

if self._col_indices is not None and len(names) != len(self._col_indices):
names = [names[i] for i in sorted(self._col_indices)]

return {name: alldata[i + offset] for i, name in enumerate(names)}, names
len_alldata = len(alldata)
return {
name: alldata[i + offset] for i, name in enumerate(names) if i < len_alldata
}, names

# legacy
def get_chunk(self, size=None):
Expand Down Expand Up @@ -473,7 +473,12 @@ def _infer_columns(self):
self._handle_usecols(columns, names)
else:
num_original_columns = len(names)
columns = [names]
if self._col_indices is not None and len(names) != len(
self._col_indices
):
columns = [[names[i] for i in sorted(self._col_indices)]]
else:
columns = [names]
else:
columns = self._handle_usecols(columns, columns[0])
else:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/parser/usecols/test_usecols_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,18 @@ def test_usecols_subset_names_mismatch_orig_columns(all_parsers, usecols):
result = parser.read_csv(StringIO(data), header=0, names=names, usecols=usecols)
expected = DataFrame({"A": [1, 5], "C": [3, 7]})
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("names", [None, ["a", "b"]])
def test_usecols_indices_out_of_bounds(all_parsers, names):
# GH#25623
parser = all_parsers
data = """
a,b
1,2
"""
result = parser.read_csv(StringIO(data), usecols=[0, 2], names=names, header=0)
expected = DataFrame({"a": [1], "b": [None]})
if names is None and parser.engine == "python":
expected = DataFrame({"a": [1]})
tm.assert_frame_equal(result, expected)