-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG/Perf: Support ExtensionArrays in where #24114
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
Changes from 3 commits
c4604df
56470c3
6f79282
a69dbb3
911a2da
badb5be
edff47e
4715ef6
d90f384
5e14414
e9665b8
033ac9c
1271d3d
9e0d87d
e05a597
796332c
cad0c4c
6edd286
30775f0
4de8bb5
ce04a75
f98a82c
bcfb8f8
8d9b20b
c0351fd
539d3cb
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 |
---|---|---|
|
@@ -64,6 +64,7 @@ class ExtensionArray(object): | |
* unique | ||
* factorize / _values_for_factorize | ||
* argsort / _values_for_argsort | ||
* where | ||
|
||
The remaining methods implemented on this class should be performant, | ||
as they only compose abstract methods. Still, a more efficient | ||
|
@@ -661,6 +662,40 @@ def take(self, indices, allow_fill=False, fill_value=None): | |
# pandas.api.extensions.take | ||
raise AbstractMethodError(self) | ||
|
||
def where(self, cond, other): | ||
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. The other implementations of |
||
""" | ||
Replace values where the condition is False. | ||
|
||
Parameters | ||
---------- | ||
cond : ndarray or ExtensionArray | ||
The mask indicating which values should be kept (True) | ||
or replaced from `other` (False). | ||
|
||
other : ndarray, ExtensionArray, or scalar | ||
Entries where `cond` is False are replaced with | ||
corresponding value from `other`. | ||
|
||
Notes | ||
----- | ||
Note that `cond` and `other` *cannot* be a Series, Index, or callable. | ||
When used from, e.g., :meth:`Series.where`, pandas will unbox | ||
Series and Indexes, and will apply callables before they arrive here. | ||
|
||
Returns | ||
------- | ||
ExtensionArray | ||
Same dtype as the original. | ||
|
||
See Also | ||
-------- | ||
Series.where : Similar method for Series. | ||
DataFrame.where : Similar method for DataFrame. | ||
""" | ||
return type(self)._from_sequence(np.where(cond, self, other), | ||
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. hmm this turns it into an array. we have much special handling for this (e.g. see .where for DTI). i think this needs to dispatch somehow. 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. oh I see you override things. ok then. |
||
dtype=self.dtype, | ||
copy=False) | ||
|
||
def copy(self, deep=False): | ||
# type: (bool) -> ExtensionArray | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -777,6 +777,17 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, | |
|
||
return self._shallow_copy(left_take, right_take) | ||
|
||
def where(self, cond, other): | ||
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 be nice to have |
||
if is_scalar(other) and isna(other): | ||
lother = rother = other | ||
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 be safe, I think this should be |
||
else: | ||
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. Can you make this an As written I think this would raise a somewhat unclear |
||
self._check_closed_matches(other, name='other') | ||
lother = other.left | ||
rother = other.right | ||
left = np.where(cond, self.left, lother) | ||
right = np.where(cond, self.right, rother) | ||
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.
left = self.left.where(cond, lother)
right = self.right.where(cond, rother)
In [2]: left = pd.date_range('2018', periods=3); left
Out[2]: DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', freq='D')
In [3]: np.where([True, False, True], left, pd.NaT)
Out[3]: array([1514764800000000000, NaT, 1514937600000000000], dtype=object) |
||
return self._shallow_copy(left, right) | ||
|
||
def value_counts(self, dropna=True): | ||
""" | ||
Returns a Series containing counts of each interval. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,11 @@ def dtype(): | |
|
||
@pytest.fixture | ||
def data(): | ||
"""Length-100 array for this type.""" | ||
"""Length-100 array for this type. | ||
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. can you copy this doc-string to the categorical one |
||
|
||
* data[0] and data[1] should both be non missing | ||
* data[0] and data[1] should not gbe equal | ||
""" | ||
raise NotImplementedError | ||
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.