Skip to content

ENH/TST/DOC: set infer_nrows for read_fwf (GH15138) #23238

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 19 commits into from
Nov 26, 2018
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ Other Enhancements
- New attribute :attr:`__git_version__` will return git commit sha of current build (:issue:`21295`).
- Compatibility with Matplotlib 3.0 (:issue:`22790`).
- Added :meth:`Interval.overlaps`, :meth:`IntervalArray.overlaps`, and :meth:`IntervalIndex.overlaps` for determining overlaps between interval-like objects (:issue:`21998`)
- :func:`read_fwf` now accepts keyword `infer_nrows` (:issue:`15138`).
- :func:`~DataFrame.to_parquet` now supports writing a ``DataFrame`` as a directory of parquet files partitioned by a subset of the columns when ``engine = 'pyarrow'`` (:issue:`23283`)
- :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` have gained the ``nonexistent`` argument for alternative handling of nonexistent times. See :ref:`timeseries.timezone_nonexistent` (:issue:`8917`)
- :meth:`Index.difference` now has an optional ``sort`` parameter to specify whether the results should be sorted if possible (:issue:`17839`)
Expand Down
42 changes: 27 additions & 15 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def _read(filepath_or_buffer, kwds):

_fwf_defaults = {
'colspecs': 'infer',
'infer_nrows': 100,
'widths': None,
}

Expand Down Expand Up @@ -718,8 +719,8 @@ def parser_f(filepath_or_buffer,
)(read_table)


def read_fwf(filepath_or_buffer, colspecs='infer',
widths=None, **kwds):
def read_fwf(filepath_or_buffer, colspecs='infer', widths=None,
infer_nrows=100, **kwds):

r"""
Read a table of fixed-width formatted lines into DataFrame.
Expand Down Expand Up @@ -752,6 +753,11 @@ def read_fwf(filepath_or_buffer, colspecs='infer',
widths : list of int, optional
A list of field widths which can be used instead of 'colspecs' if
the intervals are contiguous.
infer_nrows : int, default 100
The number of rows to consider when letting the parser determine the
`colspecs`.

.. versionadded:: 0.24.0
**kwds : optional
Optional keyword arguments can be passed to ``TextFileReader``.

Expand Down Expand Up @@ -786,6 +792,7 @@ def read_fwf(filepath_or_buffer, colspecs='infer',
col += w

kwds['colspecs'] = colspecs
kwds['infer_nrows'] = infer_nrows
kwds['engine'] = 'python-fwf'
return _read(filepath_or_buffer, kwds)

Expand Down Expand Up @@ -3442,13 +3449,15 @@ class FixedWidthReader(BaseIterator):
A reader of fixed-width lines.
"""

def __init__(self, f, colspecs, delimiter, comment, skiprows=None):
def __init__(self, f, colspecs, delimiter, comment, skiprows=None,
infer_nrows=100):
self.f = f
self.buffer = None
self.delimiter = '\r\n' + delimiter if delimiter else '\n\r\t '
self.comment = comment
if colspecs == 'infer':
self.colspecs = self.detect_colspecs(skiprows=skiprows)
self.colspecs = self.detect_colspecs(infer_nrows=infer_nrows,
skiprows=skiprows)
else:
self.colspecs = colspecs

Expand All @@ -3464,19 +3473,20 @@ def __init__(self, f, colspecs, delimiter, comment, skiprows=None):
raise TypeError('Each column specification must be '
'2 element tuple or list of integers')

def get_rows(self, n, skiprows=None):
def get_rows(self, infer_nrows, skiprows=None):
"""
Read rows from self.f, skipping as specified.

We distinguish buffer_rows (the first <= n lines)
from the rows returned to detect_colspecs because
it's simpler to leave the other locations with
skiprows logic alone than to modify them to deal
with the fact we skipped some rows here as well.
We distinguish buffer_rows (the first <= infer_nrows
lines) from the rows returned to detect_colspecs
because it's simpler to leave the other locations
with skiprows logic alone than to modify them to
deal with the fact we skipped some rows here as
well.

Parameters
----------
n : int
infer_nrows : int
Number of rows to read from self.f, not counting
rows that are skipped.
skiprows: set, optional
Expand All @@ -3496,16 +3506,16 @@ def get_rows(self, n, skiprows=None):
if i not in skiprows:
detect_rows.append(row)
buffer_rows.append(row)
if len(detect_rows) >= n:
if len(detect_rows) >= infer_nrows:
break
self.buffer = iter(buffer_rows)
return detect_rows

def detect_colspecs(self, n=100, skiprows=None):
def detect_colspecs(self, infer_nrows=100, skiprows=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I actually see another problem here. I co-opted n because it was performing the intended function of infer_nrows, but that would also break previous code that relied on calling n explicitly. I'll return the code and tack on infer_nrows at the end, like you pointed out.

# Regex escape the delimiters
delimiters = ''.join(r'\%s' % x for x in self.delimiter)
pattern = re.compile('([^%s]+)' % delimiters)
rows = self.get_rows(n, skiprows)
rows = self.get_rows(infer_nrows, skiprows)
if not rows:
raise EmptyDataError("No rows from which to infer column width")
max_len = max(map(len, rows))
Expand Down Expand Up @@ -3544,8 +3554,10 @@ class FixedWidthFieldParser(PythonParser):
def __init__(self, f, **kwds):
# Support iterators, convert to a list.
self.colspecs = kwds.pop('colspecs')
self.infer_nrows = kwds.pop('infer_nrows')
PythonParser.__init__(self, f, **kwds)

def _make_reader(self, f):
self.data = FixedWidthReader(f, self.colspecs, self.delimiter,
self.comment, self.skiprows)
self.comment, self.skiprows,
self.infer_nrows)
16 changes: 16 additions & 0 deletions pandas/tests/io/parser/test_read_fwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ def test_fwf_colspecs_None(self):
expected = DataFrame([[123456, 456], [456789, 789]])
tm.assert_frame_equal(result, expected)

def test_fwf_colspecs_infer_nrows(self):
# GH 15138
data = """\
1 2
123 98
"""
# infer_nrows == 1 should have colspec == [(2, 3), (5, 6)]
df = read_fwf(StringIO(data), header=None, infer_nrows=1)
expected = pd.DataFrame([[1, 2], [3, 8]])
tm.assert_frame_equal(df, expected)

# test for infer_nrows > number of rows
df = read_fwf(StringIO(data), header=None, infer_nrows=10)
expected = pd.DataFrame([[1, 2], [123, 98]])
tm.assert_frame_equal(df, expected)

def test_fwf_regression(self):
# GH 3594
# turns out 'T060' is parsable as a datetime slice!
Expand Down