BUG: fix IndexError when usecols selects a column not at position 0 - #67626
Open
Himesh-rupchandani wants to merge 2 commits into
Open
BUG: fix IndexError when usecols selects a column not at position 0#67626Himesh-rupchandani wants to merge 2 commits into
Himesh-rupchandani wants to merge 2 commits into
Conversation
Author
|
Hi, I've reproduced this and prepared a fix + regression test. PR is up. Summary: in |
read_csv with low_memory=True and usecols selecting a column whose position is not 0 raised IndexError when different chunks inferred mixed dtypes for that column. In _concatenate_chunks the chunk keys are raw column positions, but column_names is already filtered by usecols, so it cannot be indexed by the raw position directly. Map position -> name from the sorted chunk keys (aligned with the ascending-position order of column_names) and look up by position instead of by positional index. Adds a regression test that reproes the failure at the _concatenate_chunks level (mixed-dtype warning for a column at position 50). Closes GH#67375 Co-authored-by: Himesh Rupchandani <rupchandanihimesh69@gmail.com>
Himesh-rupchandani
force-pushed
the
fix/67375-usecols-concatenate-indexerror
branch
from
September 1, 2026 02:10
fd95694 to
7d1971b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes GH#67375.
What
read_csv(usecols=...)withlow_memory=TrueraisedIndexErrorwhen ausecols-selected column was at a position other than 0 and different chunksinferred mixed dtypes for that column.
Root cause
In
_concatenate_chunks, chunk keys are raw column positions (e.g. 50), butcolumn_namesis already filtered byusecols, socolumn_names[name]indexed by the raw position and went out of range. It only worked when the
column happened to be at position 0.
Fix
Build a position->name mapping from the sorted chunk keys (which align with
the ascending-position order of
column_names) and look up by positioninstead of positional index.
Test
Adds
test_concatenate_chunks_usecols_mixed_dtype_nonzero_posintest_concatenate_chunks.py. It fails before the fix (IndexError) and passesafter.
AI assistance
I used an AI assistant to reproduce the bug, locate the cause, and draft the
fix and test; I reviewed all changed lines and verified the behaviour locally.