-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 12 commits
057eb29
9a9609d
68d83c9
da081a8
3f69510
f3e715d
1df0493
a35abd8
d5de9d9
49d5594
6675dac
e1500cc
de29edf
c4cfd9e
0853be7
3325262
d8c307e
cc1ce14
ecf35d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,15 +343,23 @@ | |
_engine_doc)) | ||
|
||
_fwf_widths = """\ | ||
colspecs : list of pairs (int, int) or 'infer'. optional | ||
colspecs : list of pairs (int, int) or 'infer', default 'infer' | ||
A list of pairs (tuples) giving the extents of the fixed-width | ||
fields of each line as half-open intervals (i.e., [from, to[ ). | ||
fields of each line as half-open intervals (i.e., [from, to) ). | ||
String value 'infer' can be used to instruct the parser to try | ||
detecting the column specifications from the first 100 rows of | ||
the data which are not being skipped via skiprows (default='infer'). | ||
widths : list of ints. optional | ||
A list of field widths which can be used instead of 'colspecs' if | ||
the data which are not being skipped via skiprows (default='infer'), | ||
or by using the `infer_nrows` parameter. | ||
widths : list of ints, optional | ||
A list of field widths which can be used instead of `colspecs` if | ||
the intervals are contiguous. | ||
infer_nrows : int, default None | ||
The number of rows to consider when letting the parser determine the | ||
``colspecs``. If not set (or set to `None`), default behavior of 100 | ||
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. Put literals like 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. Can you point me to a guide on the use of single and double backticks in pandas or GitHub? I haven't been able to find anything definitive. Or is it just single backticks for code and double backticks for literals? Should colspecs here be in singles? |
||
rows is used. | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
delimiter : str, default ``'\t' + ' '`` | ||
Characters to consider as filler characters in the fixed-width file. | ||
Can be used to specify the filler character of the fields | ||
|
@@ -528,6 +536,7 @@ def _read(filepath_or_buffer, kwds): | |
|
||
_fwf_defaults = { | ||
'colspecs': 'infer', | ||
'infer_nrows': None, | ||
'widths': None, | ||
} | ||
|
||
|
@@ -717,7 +726,8 @@ def parser_f(filepath_or_buffer, | |
|
||
|
||
@Appender(_read_fwf_doc) | ||
def read_fwf(filepath_or_buffer, colspecs='infer', widths=None, **kwds): | ||
def read_fwf(filepath_or_buffer, colspecs='infer', widths=None, | ||
infer_nrows=None, **kwds): | ||
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. shouldn't this be 100? 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. I might be bending over to maintain backwards compatibility here (or I might be wrong), but I decided to let 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. To clarify a little further, this was the only way I could think to respect old code that explicitly set 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. this is still backward compat. If n is not None, use it, else use infer_nrows. (and let's deprecate setting n that way anyhow). |
||
# Check input arguments. | ||
if colspecs is None and widths is None: | ||
raise ValueError("Must specify either colspecs or widths") | ||
|
@@ -733,6 +743,7 @@ def read_fwf(filepath_or_buffer, colspecs='infer', widths=None, **kwds): | |
col += w | ||
|
||
kwds['colspecs'] = colspecs | ||
kwds['infer_nrows'] = infer_nrows | ||
kwds['engine'] = 'python-fwf' | ||
return _read(filepath_or_buffer, kwds) | ||
|
||
|
@@ -3363,13 +3374,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=None): | ||
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(skiprows=skiprows, | ||
infer_nrows=infer_nrows) | ||
else: | ||
self.colspecs = colspecs | ||
|
||
|
@@ -3422,10 +3435,12 @@ def get_rows(self, n, skiprows=None): | |
self.buffer = iter(buffer_rows) | ||
return detect_rows | ||
|
||
def detect_colspecs(self, n=100, skiprows=None): | ||
def detect_colspecs(self, n=100, skiprows=None, infer_nrows=None): | ||
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. Is anything actually calling this with 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. That's what I had proposed initially and had replaced Granted, that seems like a rare use case, but I was able to implement a solution that handled both cases. However, this solution seems to have come at the cost of confusing documentation, where If you and @jreback prefer, I'm happy to use either |
||
# Regex escape the delimiters | ||
delimiters = ''.join(r'\%s' % x for x in self.delimiter) | ||
pattern = re.compile('([^%s]+)' % delimiters) | ||
if infer_nrows: | ||
n = infer_nrows | ||
rows = self.get_rows(n, skiprows) | ||
if not rows: | ||
raise EmptyDataError("No rows from which to infer column width") | ||
|
@@ -3465,8 +3480,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) |
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.
can you add infer_nrows after widths. pls add a versionadded tag
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.
default 100
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.
I'm switching the places and adding a versionadded tag.
Regarding the default of
None
, I mention my reasoning in the comment forread_fwf
. I do state here in the docsIf not set (or set to `None`), default behavior of 100 rows is used.
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.
right but better to simply say its 100