Skip to content

Deprecated usecols with out of bounds indices in read_csv with c engine #41129

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ Deprecations
- Deprecated the ``level`` keyword for :class:`DataFrame` and :class:`Series` aggregations; use groupby instead (:issue:`39983`)
- The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.add_categories` is deprecated and will be removed in a future version (:issue:`37643`)
- Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)

.. ---------------------------------------------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,17 @@ cdef class TextReader:
f"{self.table_width - self.leading_cols} "
f"and found {num_cols}")

if (self.usecols is not None and not callable(self.usecols) and
all(isinstance(u, int) for u in self.usecols)):
missing_usecols = [col for col in self.usecols if col >= num_cols]
if missing_usecols:
warnings.warn(
"Defining usecols with out of bounds indices is deprecated "
"and will raise a ParserError in a future version.",
FutureWarning,
stacklevel=6,
)

results = {}
nused = 0
for i in range(self.table_width):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/io/parser/test_c_parser_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,16 @@ def test_float_precision_options(c_parser_only):

with pytest.raises(ValueError, match=msg):
parser.read_csv(StringIO(s), float_precision="junk")


def test_usecols_indices_out_of_bounds(c_parser_only):
# GH#25623
parser = c_parser_only
data = """
a,b
1,2
"""
with tm.assert_produces_warning(FutureWarning):
result = parser.read_csv(StringIO(data), usecols=[0, 2])
expected = DataFrame({"a": [1], "b": None})
tm.assert_frame_equal(result, expected)