-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Read CSV on python engine fails when skiprows and chunk size are specified (#55677, #56323) #56250
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
BUG: Read CSV on python engine fails when skiprows and chunk size are specified (#55677, #56323) #56250
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6cd0188
Fix -GH 55677:
Flytre 9233273
Fix -GH 55677:
Flytre 35a6929
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5469a47
Fix -GH 55677:
Flytre b887383
Merge remote-tracking branch 'origin/fix_55677_2' into fix_55677_2
Flytre 7d0b165
Fix -GH 55677:
Flytre 0808458
Fix -GH 55677:
Flytre c3b330e
Fix -GH 55677 & 56323:
Flytre 265bd1e
Trigger CI
Flytre c5a6fdb
Merge remote-tracking branch 'upstream/main' into fix_55677_2
Flytre f870732
Merge branch 'main' into fix_55677_2
Flytre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,3 +301,31 @@ def test_skip_rows_and_n_rows(all_parsers): | |
result = parser.read_csv(StringIO(data), nrows=5, skiprows=[2, 4, 6]) | ||
expected = DataFrame({"a": [1, 3, 5, 7, 8], "b": ["a", "c", "e", "g", "h"]}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@xfail_pyarrow | ||
def test_skip_rows_with_chunks(all_parsers): | ||
# GH 55677 | ||
data = """col_a | ||
10 | ||
20 | ||
30 | ||
40 | ||
50 | ||
60 | ||
70 | ||
80 | ||
90 | ||
100 | ||
""" | ||
parser = all_parsers | ||
reader = parser.read_csv( | ||
StringIO(data), engine=parser, skiprows=lambda x: x in [1, 4, 5], chunksize=4 | ||
) | ||
df1 = next(reader) | ||
df2 = next(reader) | ||
|
||
tm.assert_frame_equal( | ||
df1, DataFrame({"col_a": [20, 30, 60, 70]}, index=[0, 1, 2, 3]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit but you don't need to specify index here; will help condense this to one line |
||
) | ||
tm.assert_frame_equal(df2, DataFrame({"col_a": [80, 90, 100]}, index=[4, 5, 6])) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We aren't planning on removing this branch - it just serves the non-callable case right? If so I think this comment is a bit confusing so can just be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some weird behavior in this branch, and I'd argue to remove it.
Consider this csv file:
If we read this file in via read_csv:
With the python engine we get the following result:
With c engine we get a different result:
With the python engine and skiprows=lambda x: x in [1, 2, 3, 7, 10] (this PR adds this behavior, currently with these parameters pandas raises an exception):
If we remove the entire else branch and use the logic this PR adds for the 'callable' case, the python engine will match the c engine result in both cases, whether given a list or callable skiprows.
Thoughts? If you'd like to go ahead with the proposed update, should I create a separate issue for it and also link it to this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case I would go ahead and do your proposed change now. If it makes the behavior exactly match the c engine might as well do the fix all at once