Skip to content

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

17 changes: 0 additions & 17 deletions pandas/tests/base/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,23 +826,6 @@ def test_access_by_position(self, indices):
with pytest.raises(IndexError, match=msg):
series.iloc[size]

@pytest.mark.parametrize("indexer_klass", [list, pd.Index])
@pytest.mark.parametrize(
"indexer",
[
[True] * 10,
[False] * 10,
[True, False, True, True, False, False, True, True, False, True],
],
)
def test_bool_indexing(self, indexer_klass, indexer):
# GH 22533
for idx in self.indexes:
exp_idx = [i for i in range(len(indexer)) if indexer[i]]
tm.assert_index_equal(idx[indexer_klass(indexer)], idx[exp_idx])
s = pd.Series(idx)
tm.assert_series_equal(s[indexer_klass(indexer)], s.iloc[exp_idx])

def test_get_indexer_non_unique_dtype_mismatch(self):
# GH 25459
indexes, missing = pd.Index(["A", "B"]).get_indexer_non_unique(pd.Index([0]))
Expand Down
58 changes: 30 additions & 28 deletions pandas/tests/indexing/test_na_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@pytest.mark.parametrize(
"values, dtype",
[
([], "object"),
Copy link
Contributor Author

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

([1, 2, 3], "int64"),
([1.0, 2.0, 3.0], "float64"),
(["a", "b", "c"], "object"),
Expand All @@ -22,42 +23,43 @@
@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])
@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)

if not box_mask:
# Series.iloc[Series[bool]] isn't allowed
result = ser.iloc[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)

result = ser.loc[mask]
tm.assert_equal(result, expected)
expected = obj[mask]

# empty
mask = mask[:0]
ser = ser.iloc[:0]
expected = ser[mask.astype("bool")]
result = ser[mask]
result = obj[mask]
tm.assert_equal(result, expected)

if not box_mask:
# Series.iloc[Series[bool]] isn't allowed
result = ser.iloc[mask]
if indexer_class is pd.Series:
msg = "iLocation based boolean indexing cannot use an indexable as a mask"
with pytest.raises(ValueError, match=msg):
result = obj.iloc[mask]
tm.assert_equal(result, expected)
else:
result = obj.iloc[mask]
tm.assert_equal(result, expected)

result = ser.loc[mask]
result = obj.loc[mask]
tm.assert_equal(result, expected)


Expand Down