-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
API/COMPAT: support axis=None for logical reduction (reduce over all axes) #21486
Merged
jorisvandenbossche
merged 10 commits into
pandas-dev:master
from
TomAugspurger:logical-agg
Jun 26, 2018
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6114b4
Compat with NumPy 1.15 logical func
TomAugspurger 4bb6e53
whatsnew
TomAugspurger 9636d54
remove old test
TomAugspurger 18c1b11
updated docs
TomAugspurger 3c7f4e5
Merge remote-tracking branch 'upstream/master' into logical-agg
TomAugspurger 1c32b2b
Handle panel
TomAugspurger 1f94469
Skip for np 114
TomAugspurger 50db719
reuse numpy compat
TomAugspurger 9fd9740
remove xfail
TomAugspurger ae759bd
Linting
TomAugspurger 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
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 |
---|---|---|
|
@@ -1159,11 +1159,35 @@ def test_any_all(self): | |
self._check_bool_op('any', np.any, has_skipna=True, has_bool_only=True) | ||
self._check_bool_op('all', np.all, has_skipna=True, has_bool_only=True) | ||
|
||
df = DataFrame(randn(10, 4)) > 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. I would make a new test function here |
||
df.any(1) | ||
df.all(1) | ||
df.any(1, bool_only=True) | ||
df.all(1, bool_only=True) | ||
def test_any_all_extra(self): | ||
df = DataFrame({ | ||
'A': [True, False, False], | ||
'B': [True, True, False], | ||
'C': [True, True, True], | ||
}, index=['a', 'b', 'c']) | ||
result = df[['A', 'B']].any(1) | ||
expected = Series([True, True, False], index=['a', 'b', 'c']) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df[['A', 'B']].any(1, bool_only=True) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df.all(1) | ||
expected = Series([True, False, False], index=['a', 'b', 'c']) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df.all(1, bool_only=True) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# Axis is None | ||
result = df.all(axis=None).item() | ||
assert result is False | ||
|
||
result = df.any(axis=None).item() | ||
assert result is True | ||
|
||
result = df[['C']].all(axis=None).item() | ||
assert result is True | ||
|
||
# skip pathological failure cases | ||
# class CantNonzero(object): | ||
|
@@ -1185,6 +1209,86 @@ def test_any_all(self): | |
# df.any(1, bool_only=True) | ||
# df.all(1, bool_only=True) | ||
|
||
@pytest.mark.parametrize('func, data, expected', [ | ||
(np.any, {}, False), | ||
(np.all, {}, True), | ||
(np.any, {'A': []}, False), | ||
(np.all, {'A': []}, True), | ||
(np.any, {'A': [False, False]}, False), | ||
(np.all, {'A': [False, False]}, False), | ||
(np.any, {'A': [True, False]}, True), | ||
(np.all, {'A': [True, False]}, False), | ||
(np.any, {'A': [True, True]}, True), | ||
(np.all, {'A': [True, True]}, True), | ||
|
||
(np.any, {'A': [False], 'B': [False]}, False), | ||
(np.all, {'A': [False], 'B': [False]}, False), | ||
|
||
(np.any, {'A': [False, False], 'B': [False, True]}, True), | ||
(np.all, {'A': [False, False], 'B': [False, True]}, False), | ||
|
||
# other types | ||
(np.all, {'A': pd.Series([0.0, 1.0], dtype='float')}, False), | ||
(np.any, {'A': pd.Series([0.0, 1.0], dtype='float')}, True), | ||
(np.all, {'A': pd.Series([0, 1], dtype=int)}, False), | ||
(np.any, {'A': pd.Series([0, 1], dtype=int)}, True), | ||
pytest.param(np.all, {'A': pd.Series([0, 1], dtype='M8[ns]')}, False, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.any, {'A': pd.Series([0, 1], dtype='M8[ns]')}, True, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.all, {'A': pd.Series([1, 2], dtype='M8[ns]')}, True, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.any, {'A': pd.Series([1, 2], dtype='M8[ns]')}, True, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.all, {'A': pd.Series([0, 1], dtype='m8[ns]')}, False, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.any, {'A': pd.Series([0, 1], dtype='m8[ns]')}, True, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.all, {'A': pd.Series([1, 2], dtype='m8[ns]')}, True, | ||
marks=[td.skip_if_np_lt_115]), | ||
pytest.param(np.any, {'A': pd.Series([1, 2], dtype='m8[ns]')}, True, | ||
marks=[td.skip_if_np_lt_115]), | ||
(np.all, {'A': pd.Series([0, 1], dtype='category')}, False), | ||
(np.any, {'A': pd.Series([0, 1], dtype='category')}, True), | ||
(np.all, {'A': pd.Series([1, 2], dtype='category')}, True), | ||
(np.any, {'A': pd.Series([1, 2], dtype='category')}, True), | ||
|
||
# # Mix | ||
# GH-21484 | ||
# (np.all, {'A': pd.Series([10, 20], dtype='M8[ns]'), | ||
# 'B': pd.Series([10, 20], dtype='m8[ns]')}, True), | ||
]) | ||
def test_any_all_np_func(self, func, data, expected): | ||
# https://github.com/pandas-dev/pandas/issues/19976 | ||
data = DataFrame(data) | ||
result = func(data) | ||
assert isinstance(result, np.bool_) | ||
assert result.item() is expected | ||
|
||
# method version | ||
result = getattr(DataFrame(data), func.__name__)(axis=None) | ||
assert isinstance(result, np.bool_) | ||
assert result.item() is expected | ||
|
||
def test_any_all_object(self): | ||
# https://github.com/pandas-dev/pandas/issues/19976 | ||
result = np.all(DataFrame(columns=['a', 'b'])).item() | ||
assert result is True | ||
|
||
result = np.any(DataFrame(columns=['a', 'b'])).item() | ||
assert result is False | ||
|
||
@pytest.mark.parametrize('method', ['any', 'all']) | ||
def test_any_all_level_axis_none_raises(self, method): | ||
df = DataFrame( | ||
{"A": 1}, | ||
index=MultiIndex.from_product([['A', 'B'], ['a', 'b']], | ||
names=['out', 'in']) | ||
) | ||
xpr = "Must specify 'axis' when aggregating by level." | ||
with tm.assert_raises_regex(ValueError, xpr): | ||
getattr(df, method)(axis=None, level='out') | ||
|
||
def _check_bool_op(self, name, alternative, frame=None, has_skipna=True, | ||
has_bool_only=False): | ||
if frame is None: | ||
|
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
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.
Shouldn't there be an override of this in case of Panel? (which is the only case where the stat_axis differs from 0)