-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
Previously, read_fwf would infer the width of the columns from the first 100 rows of data. It now accepts a parameter infer_nrows that lets the user choose how many rows to use in the inference.
Fixed reference in colspecs. Switched half-open interval representation from non-standard [from, to[ to standard [from, to).
Hello @rdmontgomery! Thanks for submitting the PR.
|
pandas/io/parsers.py
Outdated
@@ -716,7 +720,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', infer_nrows=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.
Should add the new keyword to the end of positional arguments so as not to interfere with existing user code
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.
Thanks, @WillAyd.
pandas/io/parsers.py
Outdated
@@ -3420,11 +3428,11 @@ 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, infer_nrows, skiprows=None): |
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.
Is there a reason why you changed this from required to optional? Think that might be causing your test failures
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.
It was an oversight. Fixed now. Thanks!
Codecov Report
@@ Coverage Diff @@
## master #23238 +/- ##
==========================================
+ Coverage 92.29% 92.29% +<.01%
==========================================
Files 161 161
Lines 51498 51500 +2
==========================================
+ Hits 47530 47533 +3
+ Misses 3968 3967 -1
Continue to review full report at Codecov.
|
Hi @WillAyd, thanks for reviewing my code. I made the requested changes and it looks like everything passed now. Is there anything else I can do for this? |
pandas/io/parsers.py
Outdated
@@ -3361,13 +3367,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, infer_nrows=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.
Still need to change position of infer_nrows
to be at the end here
@@ -3420,11 +3428,11 @@ 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, infer_nrows=100, skiprows=None): |
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.
Same as above
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.
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.
Rework so that if infer_nrows is not set, detect_colspecs still uses n=100, preserving previous behavior. If infer_nrows comes down the pipe, the default n is overwritten. Also make sure that the new parameter infer_nrows is the trailing keyword in all function calls. Fix the documentation to reflect these changes.
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.
pls add a whatsnew note as well (other enhancements)
pandas/io/parsers.py
Outdated
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. | ||
infer_nrows : int, default None |
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 for read_fwf
. I do state here in the docs
If 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
pandas/io/parsers.py
Outdated
@@ -716,7 +722,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 comment
The 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 comment
The 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 infer_nrows
default to None
so that if there is old code that had explicitly called detect_colspecs(n=1234)
, the function would behave as they expected. If someone sets infer_nrows
to a number, then n
is overwritten with 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.
To clarify a little further, this was the only way I could think to respect old code that explicitly set n
in a direct call to detect_colspecs
. Any new code that chooses to use infer_nrows
will overwrite n
, but I did not want to automatically overwrite n
with a default of 100 if someone had already chosen to do otherwise.
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.
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).
pandas/io/parsers.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Put literals like None
in double backpacks
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 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?
pandas/io/parsers.py
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is anything actually calling this with n
? I believe this is an internal method so maybe here we can get by with just eliminating n
altogether and replacing it with the value from 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.
That's what I had proposed initially and had replaced n
with infer_nrows
in place. Your comment about putting the new keyword as a trailing argument is what made me realize that it was possible for someone's code to have instantiated FixedWidthReader and explicitly set n
.
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 infer_nrows
has a default of None
in order for read_fwf(colspecs='infer')
to default to inferring from the first 100 rows, as was the case beforehand.
If you and @jreback prefer, I'm happy to use either
detect_colspecs(self, infer_nrows=100, skiprows=None)
or
detect_colspecs(self, skiprows=None, infer_nrows=100)
with the awareness that it might be a breaking change in rare cases.
pandas/io/parsers.py
Outdated
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. | ||
infer_nrows : int, default None |
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
pandas/io/parsers.py
Outdated
@@ -716,7 +722,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 comment
The 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).
Preserve backwards compatibility by allowing n to be set, but the new default will be to use infer_nrows.
Thanks, guys. I appreciate your help in moving this forward. I'm not sure what happened with the CircleCI build this time around. Looks like maybe debian.org was having temporary troubles. Any advice here?
|
looks suprious. merge master and push again. |
pandas/io/parsers.py
Outdated
# 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) | ||
if n: |
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.
Since this is an internal method only and not part of the api I wonder if it wouldn't be better to just replace the n
keyword with infer_rows
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.
That sounds reasonable to me, @WillAyd. Would it be better to leave the keyword order as-is with n
removed or should I reorder like this def detect_colspecs(self, infer_nrows=100, skiprows=None):
? Or does that really not matter at all?
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'd go with the latter unless @jreback has a differing opinion
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 think ok to reorder that is an internal method
@rdmontgomery can you merge master. @WillAyd if you have any comments. |
@rdmontgomery would be curious if you have thoughts on my previous comment. Always good to simplify / cleanup internal code if possible |
pls merge master as well |
rebased. |
@rdmontgomery can you address last comment around the re-ordering of the internal function signature? Once that is done I'm OK to approve and merge |
I'll jump on it tomorrow morning! I've been in the last negotiations of a job offer and I just wrapped that up today. I've got some time again! |
Let infer_nrows replace the internal parameter n and reorder the keywords.
Fix bug introduced when master was merged back in and the function definition was changed along with the docstring.
Thanks @rdmontgomery ! Good luck with the job |
git diff upstream/master -u -- "*.py" | flake8 --diff
read_fwf
can infer the widths of columns of a dataset from a file or buffer, but if the widths change after the first 100 rows, then the default behavior fails to capture the data correctly. This PR addresses that issue by allowing the user to set a parameterinfer_nrows
that is passed down to the appropriate function.The associated test ensures that
colspecs
can be determined from the first row, thereby missing data from the subsequent rows and verifying the expected behavior.