-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Attempt to fix issue #10366 encoding and categoricals hdf serialization. #10454
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
Closed
Closed
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
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 |
---|---|---|
|
@@ -930,6 +930,51 @@ def test_encoding(self): | |
result = store.select('df',Term('columns=A',encoding='ascii')) | ||
tm.assert_frame_equal(result,expected) | ||
|
||
def test_latin_encoding(self): | ||
|
||
if compat.PY2: | ||
self.assertRaisesRegexp(TypeError, '\[unicode\] is not implemented as a table column') | ||
return | ||
|
||
values = [[b'E\xc9, 17', b'', b'a', b'b', b'c'], | ||
[b'E\xc9, 17', b'a', b'b', b'c'], | ||
[b'EE, 17', b'', b'a', b'b', b'c'], | ||
[b'E\xc9, 17', b'\xf8\xfc', b'a', b'b', b'c'], | ||
[b'', b'a', b'b', b'c'], | ||
[b'\xf8\xfc', b'a', b'b', b'c'], | ||
[b'A\xf8\xfc', b'', b'a', b'b', b'c'], | ||
[np.nan, b'', b'b', b'c'], | ||
[b'A\xf8\xfc', np.nan, b'', b'b', b'c']] | ||
|
||
def _try_decode(x, encoding='latin-1'): | ||
try: | ||
return x.decode(encoding) | ||
except AttributeError: | ||
return x | ||
# not sure how to remove latin-1 from code in python 2 and 3 | ||
values = [[_try_decode(x) for x in y] for y in values] | ||
|
||
examples = [] | ||
for dtype in ['category', object]: | ||
for val in values: | ||
examples.append(pandas.Series(val, dtype=dtype)) | ||
|
||
def roundtrip(s, key='data', encoding='latin-1', nan_rep=''): | ||
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. use the 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. Ok, I've used the self.path method here. No more mktemp. |
||
with ensure_clean_path(self.path) as store: | ||
s.to_hdf(store, key, format='table', encoding=encoding, | ||
nan_rep=nan_rep) | ||
retr = read_hdf(store, key) | ||
s_nan = s.replace(nan_rep, np.nan) | ||
assert_series_equal(s_nan, retr) | ||
|
||
for s in examples: | ||
roundtrip(s) | ||
|
||
# fails: | ||
# for x in examples: | ||
# roundtrip(s, nan_rep=b'\xf8\xfc') | ||
|
||
|
||
def test_append_some_nans(self): | ||
|
||
with ensure_clean_store(self.path) as store: | ||
|
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.
why do you think this is necessary 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.
This is from the first outdated diff above. It seemed like the old code was intended to throw an exception and trigger the except section but it was not doing this. As per your comment I replace this with the call to _convert_string_array. I think this just encodes and sets the dtype.
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 not needed
an exception is thrown on some conversions already this is duplicative
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.
Removing this line causes the tests to fail with the usual ValueError: Categorical categories must be unique. _convert_string_array seems to be throwing an error: 'bytes' object has no attribute 'encode'
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.
If you are getting these exceptions then something else might be wrong. This should simply be changed to catch a very specific exception. The problem is that this hits a very non-performant patch if it excepts and we want to avoid that by a coding error (rather than an actual exception).
This might be an old bug IIRC. So would appreciate investigation.
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 the problem is that
_convert_string_array
should return a unicode type (e.g.U
) if their is an encoding. Try changing this.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 had a quick look at this but didn't get see a way of getting around the np.vectorize line. I don't have a lot of time right now to dig into this so maybe there is a simple solution I am missing.