-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
COMPAT: Pypy tweaks #17351
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
COMPAT: Pypy tweaks #17351
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e4b763
BUG, COMPAT: list(set(self.usecols)) may not be sorted
mattip e01dd48
BUG, COMPAT: PyList_GET_SIZE works only by chance on ndarray
mattip 7003f2d
COMPAT: when "nan is nan" is not consistent across implementations
mattip 2886a0e
document the changes
mattip bb7ef63
add test that fails before sorting usecols
mattip 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ | |
import os | ||
import pandas.util.testing as tm | ||
|
||
from pandas import read_csv, read_table | ||
from pandas import read_csv, read_table, DataFrame | ||
from pandas.core.common import AbstractMethodError | ||
from pandas._libs.lib import Timestamp | ||
from pandas.compat import StringIO | ||
|
||
from .common import ParserTests | ||
from .header import HeaderTests | ||
|
@@ -100,3 +102,51 @@ def read_table(self, *args, **kwds): | |
kwds = kwds.copy() | ||
kwds['engine'] = self.engine | ||
return read_table(*args, **kwds) | ||
|
||
|
||
class TestUnsortedUsecols(object): | ||
def test_override__set_noconvert_columns(self): | ||
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. hah you are basically mocking the parser here....ok. I didn't mean go that far, but this is ok. |
||
# GH 17351 - usecols needs to be sorted in _setnoconvert_columns | ||
# based on the test_usecols_with_parse_dates test from usecols.py | ||
from pandas.io.parsers import CParserWrapper, TextFileReader | ||
|
||
s = """a,b,c,d,e | ||
0,1,20140101,0900,4 | ||
0,1,20140102,1000,4""" | ||
|
||
parse_dates = [[1, 2]] | ||
cols = { | ||
'a': [0, 0], | ||
'c_d': [ | ||
Timestamp('2014-01-01 09:00:00'), | ||
Timestamp('2014-01-02 10:00:00') | ||
] | ||
} | ||
expected = DataFrame(cols, columns=['c_d', 'a']) | ||
|
||
class MyTextFileReader(TextFileReader): | ||
def __init__(self): | ||
self._currow = 0 | ||
self.squeeze = False | ||
|
||
class MyCParserWrapper(CParserWrapper): | ||
def _set_noconvert_columns(self): | ||
if self.usecols_dtype == 'integer': | ||
# self.usecols is a set, which is documented as unordered | ||
# but in practice, a CPython set of integers is sorted. | ||
# In other implementations this assumption does not hold. | ||
# The following code simulates a different order, which | ||
# before GH 17351 would cause the wrong columns to be | ||
# converted via the parse_dates parameter | ||
self.usecols = list(self.usecols) | ||
self.usecols.reverse() | ||
return CParserWrapper._set_noconvert_columns(self) | ||
|
||
parser = MyTextFileReader() | ||
parser.options = {'usecols': [0, 2, 3], | ||
'parse_dates': parse_dates, | ||
'delimiter': ','} | ||
parser._engine = MyCParserWrapper(StringIO(s), **parser.options) | ||
df = parser.read() | ||
|
||
tm.assert_frame_equal(df, expected) |
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.
self.usecols is a
set
of the original usecols. On PyPyset
s are not sorted so "every single time" requires a sort.Running tests with this line replaces by ``usecols.reverse() will show the dependency on the list being sorted:
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.
no averse to this, but why does this matter?
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 a test that exercises the sortedness?
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.
there are tests, but they all pass on CPython since sets of ints are always sorted, ie pandas/tests/io/parsers/usecol.py, lines 199, 270, 296 which all failed on PyPy before this change. Not sure how I could construct a failing test for CPython