-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Move any/all to NDFrame, support additional arguments for Series. GH8302 #8550
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
Merged
Merged
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
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 |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
import pandas.index as _index | ||
from pandas.lib import Timestamp, Timedelta, is_datetime_array | ||
from pandas.core.base import PandasObject, FrozenList, FrozenNDArray, IndexOpsMixin, _shared_docs | ||
from pandas.util.decorators import Appender, cache_readonly, deprecate | ||
from pandas.util.decorators import (Appender, Substitution, cache_readonly, | ||
deprecate) | ||
from pandas.core.common import isnull, array_equivalent | ||
import pandas.core.common as com | ||
from pandas.core.common import (_values_from_object, is_float, is_integer, | ||
|
@@ -2088,12 +2089,13 @@ def _evaluate_with_datetime_like(self, other, op, opstr): | |
def _add_numeric_methods_disabled(cls): | ||
""" add in numeric methods to disable """ | ||
|
||
def _make_invalid_op(opstr): | ||
def _make_invalid_op(name): | ||
|
||
def _invalid_op(self, other=None): | ||
raise TypeError("cannot perform {opstr} with this index type: {typ}".format(opstr=opstr, | ||
typ=type(self))) | ||
return _invalid_op | ||
def invalid_op(self, other=None): | ||
raise TypeError("cannot perform {name} with this index type: {typ}".format(name=name, | ||
typ=type(self))) | ||
invalid_op.__name__ = name | ||
return invalid_op | ||
|
||
cls.__mul__ = cls.__rmul__ = _make_invalid_op('__mul__') | ||
cls.__floordiv__ = cls.__rfloordiv__ = _make_invalid_op('__floordiv__') | ||
|
@@ -2178,8 +2180,62 @@ def _evaluate_numeric_unary(self): | |
cls.__abs__ = _make_evaluate_unary(lambda x: np.abs(x),'__abs__') | ||
cls.__inv__ = _make_evaluate_unary(lambda x: -x,'__inv__') | ||
|
||
@classmethod | ||
def _add_logical_methods(cls): | ||
""" add in logical methods """ | ||
|
||
_doc = """ | ||
|
||
%(desc)s | ||
|
||
Parameters | ||
---------- | ||
All arguments to numpy.%(outname)s are accepted. | ||
|
||
Returns | ||
------- | ||
%(outname)s : bool or array_like (if axis is specified) | ||
A single element array_like may be converted to bool.""" | ||
|
||
def _make_logical_function(name, desc, f): | ||
|
||
@Substitution(outname=name, desc=desc) | ||
@Appender(_doc) | ||
def logical_func(self, *args, **kwargs): | ||
result = f(self.values) | ||
if isinstance(result, (np.ndarray, com.ABCSeries, Index)) \ | ||
and result.ndim == 0: | ||
# return NumPy type | ||
return result.dtype.type(result.item()) | ||
else: # pragma: no cover | ||
return result | ||
logical_func.__name__ = name | ||
return logical_func | ||
|
||
cls.all = _make_logical_function( | ||
'all', 'Return whether all elements are True', np.all) | ||
cls.any = _make_logical_function( | ||
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. much better! 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. Thanks! |
||
'any', 'Return whether any element is True', np.any) | ||
|
||
@classmethod | ||
def _add_logical_methods_disabled(cls): | ||
""" add in logical methods to disable """ | ||
|
||
def _make_invalid_op(name): | ||
|
||
def invalid_op(self, other=None): | ||
raise TypeError("cannot perform {name} with this index type: {typ}".format(name=name, | ||
typ=type(self))) | ||
invalid_op.__name__ = name | ||
return invalid_op | ||
|
||
cls.all = _make_invalid_op('all') | ||
cls.any = _make_invalid_op('any') | ||
|
||
|
||
Index._add_numeric_methods_disabled() | ||
Index._add_logical_methods() | ||
|
||
|
||
class NumericIndex(Index): | ||
""" | ||
|
@@ -2291,7 +2347,11 @@ def equals(self, other): | |
def _wrap_joined_index(self, joined, other): | ||
name = self.name if self.name == other.name else None | ||
return Int64Index(joined, name=name) | ||
|
||
|
||
Int64Index._add_numeric_methods() | ||
Int64Index._add_logical_methods() | ||
|
||
|
||
class Float64Index(NumericIndex): | ||
|
||
|
@@ -2483,7 +2543,10 @@ def isin(self, values, level=None): | |
self._validate_index_level(level) | ||
return lib.ismember_nans(self._array_values(), value_set, | ||
isnull(list(value_set)).any()) | ||
|
||
|
||
Float64Index._add_numeric_methods() | ||
Float64Index._add_logical_methods_disabled() | ||
|
||
|
||
class MultiIndex(Index): | ||
|
@@ -4436,7 +4499,11 @@ def isin(self, values, level=None): | |
return np.zeros(len(labs), dtype=np.bool_) | ||
else: | ||
return np.lib.arraysetops.in1d(labs, sought_labels) | ||
|
||
|
||
MultiIndex._add_numeric_methods_disabled() | ||
MultiIndex._add_logical_methods_disabled() | ||
|
||
|
||
# For utility purposes | ||
|
||
|
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
Oops, something went wrong.
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 is this necessary?
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.
These logical functions have slightly different documentation: a 'bool_only' field instead of a 'numeric_only' field, and potentially a message about supporting additional numpy arguments via the bool_extended_args variable.
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 don't think this is necessary (nor is any other 'bool_extended_args' variable), its just not needed. That said If you have a use case pls show it.
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'd like to better understand your comment. I mentioned two special cases in the documentation here, the bool_only argument name and the documentation message about supporting additional numpy arguments, which are currently supported in master's Series.any/all, which forwards to numpy's any/all.
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.
maybe I wasn't clear. The signature should be something like:
any(laxis, skipna, level, **kwargs)
; we accept but don't deal explicity with the numpy args (e.g.out
) and such. Its not useful, nor is it consistent with how pandas works.I realized I was confusing about
bool_only
, yes that is a good idea (just notbool_extended_args
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.
Thanks, got it.
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.
shouldn't
bool_only=True
be the default?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 made bool_only default to None (False) here because:
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, though I find its a little odd (e.g. I can't think of a reason to use
any/all
on non-boolean data), but I am sure people will try it!.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 guess, also, if they're calling any/all on Series in master, they are using the numpy version which does not offer a bool_only option.
Would it make sense for me to create a new issue for implementing bool_only (numeric_only) in Series an Panel, and look into whether it should be enabled by default for any/all?