-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Added colspecs detection to read_fwf #4955
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
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ pandas/io/*.json | |
|
||
.project | ||
.pydevproject | ||
.settings |
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
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
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.
so this one actually needs to work on Python 2 as well. I know it's a little annoying to do that. Also, can this handle non-utf8?
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 just copied it from
test_BytesIO_input
line 2103 which also skips it.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.
And also the problem is that in Python2 i don't get a unicode string from the reader even if I pass the encoding. I think this is a bug on the reader :-/
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 sure this isn't the case, but are you sure it isn't because we've missed a function call somewhere that was supposed to get an encoding?
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 sure that in the
FixedWidthReader
when I callnext(self.f)
i get<type 'str'>
:) Is that what I should get?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.
@jtratner I'm not familiar with the
read1
issue.And the problem about the
StringIO
is the following:If I get a
StringIO
object I have no idea if I will get bytes or strings from it.On the other hand:
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.
not a huge deal, encountered it in other places. anyways, many many test cases use
StringIO(some_str)
to pass into functions, so we can't really deprecate support for it.Help me out here though - why does it actually matter to wrap in Python 2? The issue in Python 3 is that you can't use string methods on bytes, but in Python 2 that's not a problem. Anyways, we should probably put this in a separate issue so we don't get too sidetracked here.
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.
@jtratner "The issue in Python 3 is that you can't use string methods on bytes, but in Python 2 that's not a problem."
This is not true. Yes, it's true that you can use them, but they won't produce a correct result. Here is a simple example:
The method wont raises an error, but the result won't be correct. That's why it's important to wrap everything to be unicode. None of the
Series.str
methods would work correctly with python 2 strings if you have unicode data. Example:I encountered the same problem when I wrote the column detection for
read_fwf
function. Unless the strings are unicode, I cannot determine the correct widths of the columns for variable byte encodings likeutf-8
.Yes, of course I can use the encoding and check if the thing I got is
bytes
and then decode it using the encoding, calculate the thing I need, and so on. But that is not DRY. You will have to do that all over the place in every method that does something with strings.Instead of that, my proposal is to fix the
PythonParser.__init__
function to create a stream object (f
) that will always return unicode strings, and have the logic only in one place. The same thing would apply for the C parser.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.
Yes, that's basically what is already the case with Python 3. (you might have to put it in two places because of how things are currently set up). So are you thinking you'd always read StringIO fully (or at least one line) and then handle appropriately (remember there are both cStringIO and StringIO in Python 2)? then after that you could use TextIOWrapper, right?
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.
Actually there are 3 :D -
cStringIO.StringIO
which accepts onlybytes
or strings that can be decoded using theascii
encoding,StringIO.StringIO
that accepts bothbytes
andstr
and returns the same thing it was created with, andio.StringIO
which accepts onlystr
(I'm using the python 3 terminology sostr
==unicode
).Yes the idea is to create something like the django's
safe_str
method, butSafeStringIO
that will accept any of them and always emit unicode...But I'll create a new issue and we can discuss that there, I'll reference this conversation from there.