-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: Remove redundant index test from tests/base/test_ops.py #32484
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
jreback
merged 7 commits into
pandas-dev:master
from
SaturnFromTitan:remove-redundant-index-test-from-tests-base
Mar 14, 2020
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d93ec0
removed redundant index test from tests/base/test_ops.py
SaturnFromTitan 5a7c1e4
Merge branch 'master' into remove-redundant-index-test-from-tests-base
SaturnFromTitan 2162158
refactored and split test_series_mask_boolean
SaturnFromTitan c724ba0
tightened the pytest.raises condition
SaturnFromTitan 46cc883
renaming and added a comment
SaturnFromTitan 3b4c786
removed some redundancy in the test case
SaturnFromTitan 14ea372
Revert "removed some redundancy in the test case"
SaturnFromTitan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from contextlib import nullcontext as do_not_raise | ||
|
||
import pytest | ||
|
||
import pandas as pd | ||
|
@@ -7,6 +9,7 @@ | |
@pytest.mark.parametrize( | ||
"values, dtype", | ||
[ | ||
([], "object"), | ||
([1, 2, 3], "int64"), | ||
([1.0, 2.0, 3.0], "float64"), | ||
(["a", "b", "c"], "object"), | ||
|
@@ -22,42 +25,44 @@ | |
@pytest.mark.parametrize( | ||
"mask", [[True, False, False], [True, True, True], [False, False, False]] | ||
) | ||
@pytest.mark.parametrize("box_mask", [True, False]) | ||
@pytest.mark.parametrize("indexer_class", [list, pd.array, pd.Index, pd.Series]) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@pytest.mark.parametrize("frame", [True, False]) | ||
def test_series_mask_boolean(values, dtype, mask, box_mask, frame): | ||
ser = pd.Series(values, dtype=dtype, index=["a", "b", "c"]) | ||
if frame: | ||
ser = ser.to_frame() | ||
mask = pd.array(mask, dtype="boolean") | ||
if box_mask: | ||
mask = pd.Series(mask, index=ser.index) | ||
|
||
expected = ser[mask.astype("bool")] | ||
def test_series_mask_boolean(values, dtype, mask, indexer_class, frame): | ||
# In case len(values) < 3 | ||
index = ["a", "b", "c"][: len(values)] | ||
mask = mask[: len(values)] | ||
|
||
result = ser[mask] | ||
tm.assert_equal(result, expected) | ||
obj = pd.Series(values, dtype=dtype, index=index) | ||
if frame: | ||
if len(values) == 0: | ||
# Otherwise obj is an empty DataFrame with shape (0, 1) | ||
obj = pd.DataFrame(dtype=dtype) | ||
else: | ||
obj = obj.to_frame() | ||
|
||
if indexer_class is pd.array: | ||
mask = pd.array(mask, dtype="boolean") | ||
elif indexer_class is pd.Series: | ||
mask = pd.Series(mask, index=obj.index, dtype="boolean") | ||
else: | ||
mask = indexer_class(mask) | ||
|
||
if not box_mask: | ||
# Series.iloc[Series[bool]] isn't allowed | ||
result = ser.iloc[mask] | ||
tm.assert_equal(result, expected) | ||
expected = obj[mask] | ||
|
||
result = ser.loc[mask] | ||
result = obj[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
# empty | ||
mask = mask[:0] | ||
ser = ser.iloc[:0] | ||
expected = ser[mask.astype("bool")] | ||
result = ser[mask] | ||
tm.assert_equal(result, expected) | ||
if indexer_class is pd.Series: | ||
msg = "iLocation based boolean indexing cannot use an indexable as a mask" | ||
expectation = pytest.raises(ValueError, match=msg) | ||
else: | ||
expectation = do_not_raise() | ||
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. inspired by pytest-dev/pytest#1830 |
||
|
||
if not box_mask: | ||
# Series.iloc[Series[bool]] isn't allowed | ||
result = ser.iloc[mask] | ||
with expectation: | ||
result = obj.iloc[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
result = ser.loc[mask] | ||
result = obj.loc[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
|
||
|
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.
The empty scenario was previously covered with some redundant code in the bottom of the test. I moved it to the parametrization instead and adjusted the generic test code to handle it properly