-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG: pandas.Series.unique() does not return correct unique values on non UTF8 enodeable strings #58215
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
BUG: pandas.Series.unique() does not return correct unique values on non UTF8 enodeable strings #58215
Changes from 8 commits
Commits
Show all changes
10 commits Select commit Hold shift + click to select a range
c300b46 Resolve Heisenbug in StringHashTable._unique
MichaelTiemannOSC b4157f0 Added test for #45929 and removed superfluous single_cpu mark
MichaelTiemannOSC fd9f388 Merge branch 'main' into issue_45929
MichaelTiemannOSC f33cdc0 Update v2.2.0.rst
MichaelTiemannOSC e687204 Merge remote-tracking branch 'upstream/main' into issue_45929
mroeschke 9abe522 Add comment and clear our references
mroeschke 043fc35 Add whatsnew
mroeschke 0de9f5f More exact test
mroeschke 5d87ebc Use CAPIs
mroeschke b78c6cb Remove param
mroeschke 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
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.
This doesn't fix the underlying issue. It may make it less likely but the lifecycle is still not properly controlled
I think what needs to be done is something akin to:
I think you can just do that directly in Cython, but we can look at the underlying C API if needed
(side note - I would really love to get rid of
get_c_string- not a value added layer of indirection)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 was able to replace
get_c_stringwith the associated C APIs, but if I understand https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#caveats-when-using-a-python-string-in-a-c-context correctly, since we're storing char pointers from these encoded python strings and using them later, we need to keep a reference to those Python stringsUh oh!
There was an error while loading. Please reload this page.
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 OK I see the difference - CPython can cache the UTF8 bytes of a string alongside the string object. When the string is not utf8 encodable and we have to create temporary objects we run into trouble. So the list here artificially extends the lifetime of new objects guaranteed to be utf8 encodable to rely on that caching mechanism
To be honest I would be -1 on this change and would rather call it a wontfix. It is a very niche issue that fights the internals of CPython (and for that matter pyarrow, whose strings are utf8).
If someone wanted mixed-encoding Python strings like this I think pa.binary() is a better data type choice
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.
OK I'll close this PR then. My main motivation was to make
test_unique_bad_unicodenot flaky due to this issue, but I'll open a follow up PR making this test permanentlyxfail(strict=True)insteadThere 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 name test_unique_bad_unicode is a misnomer. The code point "\ud83d" exists in Unicode. It is a high surrogate
https://www.unicode.org/charts/PDF/UD800.pdf
The problem is that by itself that high surrogate doesn't mean anything (it would need to be paired with a low surrogate). As such, it doesn't represent any glyph in any encoding.
AFAIU if you wanted to keep that unicode code point, you would have to:
str.encode(<encoding>, errors="surrogatepass")I realize step 2 may not exist today but is a good impetus to work on interop with pa.binary() if this is required