Skip to content

BUG: read_fwf not handling skiprows correctly with iterator #44621

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
Nov 26, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ I/O
- Bug in unpickling a :class:`Index` with object dtype incorrectly inferring numeric dtypes (:issue:`43188`)
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
- Bug in :func:`read_csv`, changed exception class when expecting a file path name or file-like object from ``OSError`` to ``TypeError`` (:issue:`43366`)
- Bug in :func:`read_csv` and :func:`read_fwf` ignoring all ``skiprows`` except first when ``nrows`` is specified for ``engine='python'`` (:issue:`44021`)
- Bug in :func:`read_csv` and :func:`read_fwf` ignoring all ``skiprows`` except first when ``nrows`` is specified for ``engine='python'`` (:issue:`44021`, :issue:`10261`)
- Bug in :func:`read_json` not handling non-numpy dtypes correctly (especially ``category``) (:issue:`21892`, :issue:`33205`)
- Bug in :func:`json_normalize` where multi-character ``sep`` parameter is incorrectly prefixed to every key (:issue:`43831`)
- Bug in :func:`json_normalize` where reading data with missing multi-level metadata would not respect errors="ignore" (:issue:`44312`)
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ def _get_lines(self, rows=None):
assert self.data is not None
new_rows.append(next(self.data))

len_new_rows = len(new_rows)
new_rows = self._remove_skipped_rows(new_rows)
lines.extend(new_rows)
else:
Expand All @@ -1059,13 +1060,15 @@ def _get_lines(self, rows=None):

if new_row is not None:
new_rows.append(new_row)
len_new_rows = len(new_rows)

except StopIteration:
len_new_rows = len(new_rows)
new_rows = self._remove_skipped_rows(new_rows)
lines.extend(new_rows)
if len(lines) == 0:
raise
self.pos += len(new_rows)
self.pos += len_new_rows

self.buf = []
else:
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/io/parser/test_read_fwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,33 @@ def test_skip_rows_and_n_rows():
result = read_fwf(StringIO(data), nrows=4, skiprows=[2, 4])
expected = DataFrame({"a": [1, 3, 5, 6], "b": ["a", "c", "e", "f"]})
tm.assert_frame_equal(result, expected)


def test_skiprows_with_iterator():
# GH#10261
data = """0
1
2
3
4
5
6
7
8
9
"""
df_iter = read_fwf(
StringIO(data),
colspecs=[(0, 2)],
names=["a"],
iterator=True,
chunksize=2,
skiprows=[0, 1, 2, 6, 9],
)
expected_frames = [
DataFrame({"a": [3, 4]}),
DataFrame({"a": [5, 7, 8]}, index=[2, 3, 4]),
DataFrame({"a": []}, index=[], dtype="object"),
]
for i, result in enumerate(df_iter):
tm.assert_frame_equal(result, expected_frames[i])