-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Fixes np.unique on SparseArray #19651
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b4a6359
First pass at having unique work for sparse array
hexgnu 90a0b3c
First pass at fixing group by sparse data frames
hexgnu 911c265
Cleanup hashtable_class_helper.pxi.in
hexgnu d9d643b
uint's NA value is 0, so set it as such
hexgnu 6666cd6
Add whatsnew entry
hexgnu 77e6754
Reference issue number in tests
hexgnu 6f242ee
Reference issue number in tests
hexgnu 5b39aa2
Just pass in appended unique array
hexgnu af7a804
Take out ipdb debug statement
hexgnu a716ffb
Merge remote-tracking branch 'upstream/master' into fix_groupby_sparse
hexgnu 4a13a75
Revert change on groupby
hexgnu 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 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 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 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 |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
from datetime import datetime | ||
from itertools import permutations | ||
from pandas import (Series, Categorical, CategoricalIndex, | ||
Timestamp, DatetimeIndex, Index, IntervalIndex) | ||
Timestamp, DatetimeIndex, Index, IntervalIndex, | ||
SparseArray) | ||
import pandas as pd | ||
|
||
from pandas import compat | ||
|
@@ -268,6 +269,16 @@ def test_object_refcount_bug(self): | |
for i in range(1000): | ||
len(algos.unique(lst)) | ||
|
||
@pytest.mark.parametrize('fill_value', [0, 1, np.nan, None]) | ||
def test_sparse(self, fill_value): | ||
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. Reference issue number. |
||
# GH 19595 | ||
arr = SparseArray([0, 1, np.nan, None], fill_value=fill_value) | ||
|
||
result = algos.unique(arr) | ||
|
||
assert isinstance(result, np.ndarray) | ||
assert len(result) == 3 | ||
|
||
def test_on_index_object(self): | ||
|
||
mindex = pd.MultiIndex.from_arrays([np.arange(5).repeat(5), np.tile( | ||
|
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.
not sure you saw my comment. this should not be handling unique like this. SparseArray should have a method
.unique()
which can directly handle the unique (and then call algorithms.unique on the sparse values).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.
IOW you might be able to get aways with doing a
need to avoid recursion, but here values must be a ndarray or an EA, including Categorical. (and NOT a Series)
cc @TomAugspurger
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.
Hrm I'll have to think about this... I'm hesitant to do this mainly because this code seems to rely on the fact that it always outputs an ndarray, which is why groupby doesn't work with sparse data.
The problem really is with classes that implement their own unique(). It usually outputs something that isn't ndarray.
Also I don't want to call unique on the class and cast it back to ndarray cause that feels sloppy. I'll think of something :) thanks!
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.
@hexgnu have you been following the ExtensionArray stuff? Eventually
SparseArray
should be anExtensionArray
likeCategorical
. Eventually things likegroupby
,factorize
, etc will all work correctly on EAs.SparseArray.unique() should return a SparseArray, just like Categorical.unique returns a Categorical.