Skip to content

TST/REF: misplaced Index.putmask tests #43906

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 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions pandas/tests/indexes/interval/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pandas import (
IntervalIndex,
Series,
date_range,
)
import pandas._testing as tm
from pandas.tests.indexes.common import Base
Expand Down Expand Up @@ -66,29 +65,3 @@ def test_getitem_2d_deprecated(self, simple_index):
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
with tm.assert_produces_warning(FutureWarning):
idx[:, None]


class TestPutmask:
@pytest.mark.parametrize("tz", ["US/Pacific", None])
def test_putmask_dt64(self, tz):
# GH#37968
dti = date_range("2016-01-01", periods=9, tz=tz)
idx = IntervalIndex.from_breaks(dti)
mask = np.zeros(idx.shape, dtype=bool)
mask[0:3] = True

result = idx.putmask(mask, idx[-1])
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
tm.assert_index_equal(result, expected)

def test_putmask_td64(self):
# GH#37968
dti = date_range("2016-01-01", periods=9)
tdi = dti - dti[0]
idx = IntervalIndex.from_breaks(tdi)
mask = np.zeros(idx.shape, dtype=bool)
mask[0:3] = True

result = idx.putmask(mask, idx[-1])
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
tm.assert_index_equal(result, expected)
26 changes: 26 additions & 0 deletions pandas/tests/indexes/interval/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,29 @@ def test_slice_locs_with_ints_and_floats_errors(self, tuples, query):
),
):
index.slice_locs(start, stop)


class TestPutmask:
@pytest.mark.parametrize("tz", ["US/Pacific", None])
def test_putmask_dt64(self, tz):
# GH#37968
dti = date_range("2016-01-01", periods=9, tz=tz)
idx = IntervalIndex.from_breaks(dti)
mask = np.zeros(idx.shape, dtype=bool)
mask[0:3] = True

result = idx.putmask(mask, idx[-1])
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
tm.assert_index_equal(result, expected)

def test_putmask_td64(self):
# GH#37968
dti = date_range("2016-01-01", periods=9)
tdi = dti - dti[0]
idx = IntervalIndex.from_breaks(tdi)
mask = np.zeros(idx.shape, dtype=bool)
mask[0:3] = True

result = idx.putmask(mask, idx[-1])
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
tm.assert_index_equal(result, expected)
31 changes: 22 additions & 9 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,31 @@ def test_slice_locs_with_missing_value(
assert result == expected


def test_putmask_with_wrong_mask(idx):
# GH18368
class TestPutmask:
def test_putmask_with_wrong_mask(self, idx):
# GH18368

msg = "putmask: mask and data must be the same size"
with pytest.raises(ValueError, match=msg):
idx.putmask(np.ones(len(idx) + 1, np.bool_), 1)
msg = "putmask: mask and data must be the same size"
with pytest.raises(ValueError, match=msg):
idx.putmask(np.ones(len(idx) + 1, np.bool_), 1)

with pytest.raises(ValueError, match=msg):
idx.putmask(np.ones(len(idx) - 1, np.bool_), 1)

with pytest.raises(ValueError, match=msg):
idx.putmask("foo", 1)

def test_putmask_multiindex_other(self):
# GH#43212 `value` is also a MultiIndex

left = MultiIndex.from_tuples([(np.nan, 6), (np.nan, 6), ("a", 4)])
right = MultiIndex.from_tuples([("a", 1), ("a", 1), ("d", 1)])
mask = np.array([True, True, False])

with pytest.raises(ValueError, match=msg):
idx.putmask(np.ones(len(idx) - 1, np.bool_), 1)
result = left.putmask(mask, right)

with pytest.raises(ValueError, match=msg):
idx.putmask("foo", 1)
expected = MultiIndex.from_tuples([right[0], right[1], left[2]])
tm.assert_index_equal(result, expected)


class TestGetIndexer:
Expand Down
17 changes: 0 additions & 17 deletions pandas/tests/indexes/multi/test_putmask.py

This file was deleted.

2 changes: 1 addition & 1 deletion pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def test_where(self, klass, index):
result = index.where(klass(cond))
tm.assert_index_equal(result, expected)

def test_where_uin64(self):
def test_where_uint64(self):
idx = UInt64Index([0, 6, 2])
mask = np.array([False, True, False])
other = np.array([1], dtype=np.int64)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/ranges/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,17 @@ def test_take_fill_value(self):
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
with pytest.raises(IndexError, match=msg):
idx.take(np.array([1, -5]))


class TestWhere:
def test_where_putmask_range_cast(self):
# GH#43240
idx = RangeIndex(0, 5, name="test")

mask = np.array([True, True, False, False, False])
result = idx.putmask(mask, 10)
expected = Int64Index([10, 10, 2, 3, 4], name="test")
tm.assert_index_equal(result, expected)

result = idx.where(~mask, 10)
tm.assert_index_equal(result, expected)
14 changes: 0 additions & 14 deletions pandas/tests/indexes/ranges/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,3 @@ def test_symmetric_difference(self):
result = left.symmetric_difference(right[1:])
expected = Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14])
tm.assert_index_equal(result, expected)

def test_putmask_range_cast(self):
# GH#43240
idx = RangeIndex(0, 5, name="test")
result = idx.putmask(np.array([True, True, False, False, False]), 10)
expected = Index([10, 10, 2, 3, 4], name="test")
tm.assert_index_equal(result, expected)

def test_where_range_cast(self):
# GH#43240
idx = RangeIndex(0, 5, name="test")
result = idx.where(np.array([False, False, True, True, True]), 10)
expected = Index([10, 10, 2, 3, 4], name="test")
tm.assert_index_equal(result, expected)