-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: ExtensionArray.unique #19869
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
ENH: ExtensionArray.unique #19869
Changes from 1 commit
7267544
07148db
c8b5852
5099573
a5d6b67
011d02e
b8711d3
c15d42d
a260d35
51f8a27
fc04612
41dd128
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,18 @@ def isna(self): | |
""" | ||
raise AbstractMethodError(self) | ||
|
||
def unique(self): | ||
"""Compute the ExtensionArray of unique values. | ||
|
||
Returns | ||
------- | ||
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. future PR should prob add some examples here :> (and other doc-strings). 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. True :) The only problem is that for ExtensionArray we don't have a direct working example, as you first need to subclass it (unless we use one of the existing ones like Categorical, but that also seems a bit strange) |
||
uniques : ExtensionArray | ||
""" | ||
from pandas import unique | ||
|
||
uniques = unique(self.astype(object)) | ||
return type(self)(uniques) | ||
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. i think you should add a 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. To avoid the copy or to avoid the In this specific case, I think a copy is necessary since As for alternative constructors to avoid the For my IPAddress stuff I want to be able to do zero-copy construction from
Putting those in separate constructors makes sense. 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. no its the idiom , see below, should have a 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. @jreback What do you mean with "this handled the sub-classing things" 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. because everywhere else where use 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. I understand that, but it's not because we do it everywhere else internally that we need to do it here as well. Here we have something that is part of an external interface, and if adding something like that we should have a reason for it. And a clear message to the implementer what we expect from it. 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. This just becomes confusing. I don't any reason not to have a 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. You want me to change it to 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. see my comments on that PR 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. so this now should change to |
||
|
||
# ------------------------------------------------------------------------ | ||
# Indexing methods | ||
# ------------------------------------------------------------------------ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,14 @@ def test_count(self, data_missing): | |
def test_apply_simple_series(self, data): | ||
result = pd.Series(data).apply(id) | ||
assert isinstance(result, pd.Series) | ||
|
||
@pytest.mark.parametrize('box', [pd.Series, lambda x: x]) | ||
@pytest.mark.parametrize('method', [lambda x: x.unique(), pd.unique]) | ||
def test_unique(self, data, box, method): | ||
duplicated = box(type(data)([data[0], data[0]])) | ||
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. should this one also be |
||
|
||
result = method(duplicated) | ||
|
||
assert len(result) == 1 | ||
assert isinstance(result, type(data)) | ||
assert result[0] == duplicated[0] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,13 @@ def take(self, indexer, allow_fill=True, fill_value=None): | |
def copy(self, deep=False): | ||
return type(self)(self.data[:]) | ||
|
||
def unique(self): | ||
# Parent method doesn't work since np.array will try to infer | ||
# a 2-dim object. | ||
return type(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. use 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. would change this too 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. see above comment, define _consturct_from_sequence |
||
dict(x) for x in list(set(tuple(d.items()) for d in self.data)) | ||
]) | ||
|
||
@property | ||
def _na_value(self): | ||
return {} | ||
|
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.
Ha, this
getattr
is doing nothing since it's'.values'
and not'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.
Ah, and we have a test that depends on it! I'm going to split this off to a new issue then.
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.
Ah, nevermind. I think we can just remove it instead of correcting it.