Skip to content

Commit 42a6604

Browse files
authored
TST/REF: misplaced Index.putmask tests (#43906)
1 parent 71edf6a commit 42a6604

File tree

7 files changed

+63
-68
lines changed

7 files changed

+63
-68
lines changed

pandas/tests/indexes/interval/test_base.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pandas import (
55
IntervalIndex,
66
Series,
7-
date_range,
87
)
98
import pandas._testing as tm
109
from pandas.tests.indexes.common import Base
@@ -66,29 +65,3 @@ def test_getitem_2d_deprecated(self, simple_index):
6665
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
6766
with tm.assert_produces_warning(FutureWarning):
6867
idx[:, None]
69-
70-
71-
class TestPutmask:
72-
@pytest.mark.parametrize("tz", ["US/Pacific", None])
73-
def test_putmask_dt64(self, tz):
74-
# GH#37968
75-
dti = date_range("2016-01-01", periods=9, tz=tz)
76-
idx = IntervalIndex.from_breaks(dti)
77-
mask = np.zeros(idx.shape, dtype=bool)
78-
mask[0:3] = True
79-
80-
result = idx.putmask(mask, idx[-1])
81-
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
82-
tm.assert_index_equal(result, expected)
83-
84-
def test_putmask_td64(self):
85-
# GH#37968
86-
dti = date_range("2016-01-01", periods=9)
87-
tdi = dti - dti[0]
88-
idx = IntervalIndex.from_breaks(tdi)
89-
mask = np.zeros(idx.shape, dtype=bool)
90-
mask[0:3] = True
91-
92-
result = idx.putmask(mask, idx[-1])
93-
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
94-
tm.assert_index_equal(result, expected)

pandas/tests/indexes/interval/test_indexing.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,29 @@ def test_slice_locs_with_ints_and_floats_errors(self, tuples, query):
497497
),
498498
):
499499
index.slice_locs(start, stop)
500+
501+
502+
class TestPutmask:
503+
@pytest.mark.parametrize("tz", ["US/Pacific", None])
504+
def test_putmask_dt64(self, tz):
505+
# GH#37968
506+
dti = date_range("2016-01-01", periods=9, tz=tz)
507+
idx = IntervalIndex.from_breaks(dti)
508+
mask = np.zeros(idx.shape, dtype=bool)
509+
mask[0:3] = True
510+
511+
result = idx.putmask(mask, idx[-1])
512+
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
513+
tm.assert_index_equal(result, expected)
514+
515+
def test_putmask_td64(self):
516+
# GH#37968
517+
dti = date_range("2016-01-01", periods=9)
518+
tdi = dti - dti[0]
519+
idx = IntervalIndex.from_breaks(tdi)
520+
mask = np.zeros(idx.shape, dtype=bool)
521+
mask[0:3] = True
522+
523+
result = idx.putmask(mask, idx[-1])
524+
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
525+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/multi/test_indexing.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,31 @@ def test_slice_locs_with_missing_value(
136136
assert result == expected
137137

138138

139-
def test_putmask_with_wrong_mask(idx):
140-
# GH18368
139+
class TestPutmask:
140+
def test_putmask_with_wrong_mask(self, idx):
141+
# GH18368
141142

142-
msg = "putmask: mask and data must be the same size"
143-
with pytest.raises(ValueError, match=msg):
144-
idx.putmask(np.ones(len(idx) + 1, np.bool_), 1)
143+
msg = "putmask: mask and data must be the same size"
144+
with pytest.raises(ValueError, match=msg):
145+
idx.putmask(np.ones(len(idx) + 1, np.bool_), 1)
146+
147+
with pytest.raises(ValueError, match=msg):
148+
idx.putmask(np.ones(len(idx) - 1, np.bool_), 1)
149+
150+
with pytest.raises(ValueError, match=msg):
151+
idx.putmask("foo", 1)
152+
153+
def test_putmask_multiindex_other(self):
154+
# GH#43212 `value` is also a MultiIndex
155+
156+
left = MultiIndex.from_tuples([(np.nan, 6), (np.nan, 6), ("a", 4)])
157+
right = MultiIndex.from_tuples([("a", 1), ("a", 1), ("d", 1)])
158+
mask = np.array([True, True, False])
145159

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

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

152165

153166
class TestGetIndexer:

pandas/tests/indexes/multi/test_putmask.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

pandas/tests/indexes/numeric/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def test_where(self, klass, index):
406406
result = index.where(klass(cond))
407407
tm.assert_index_equal(result, expected)
408408

409-
def test_where_uin64(self):
409+
def test_where_uint64(self):
410410
idx = UInt64Index([0, 6, 2])
411411
mask = np.array([False, True, False])
412412
other = np.array([1], dtype=np.int64)

pandas/tests/indexes/ranges/test_indexing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,17 @@ def test_take_fill_value(self):
7777
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
7878
with pytest.raises(IndexError, match=msg):
7979
idx.take(np.array([1, -5]))
80+
81+
82+
class TestWhere:
83+
def test_where_putmask_range_cast(self):
84+
# GH#43240
85+
idx = RangeIndex(0, 5, name="test")
86+
87+
mask = np.array([True, True, False, False, False])
88+
result = idx.putmask(mask, 10)
89+
expected = Int64Index([10, 10, 2, 3, 4], name="test")
90+
tm.assert_index_equal(result, expected)
91+
92+
result = idx.where(~mask, 10)
93+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/ranges/test_setops.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,3 @@ def test_symmetric_difference(self):
354354
result = left.symmetric_difference(right[1:])
355355
expected = Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14])
356356
tm.assert_index_equal(result, expected)
357-
358-
def test_putmask_range_cast(self):
359-
# GH#43240
360-
idx = RangeIndex(0, 5, name="test")
361-
result = idx.putmask(np.array([True, True, False, False, False]), 10)
362-
expected = Index([10, 10, 2, 3, 4], name="test")
363-
tm.assert_index_equal(result, expected)
364-
365-
def test_where_range_cast(self):
366-
# GH#43240
367-
idx = RangeIndex(0, 5, name="test")
368-
result = idx.where(np.array([False, False, True, True, True]), 10)
369-
expected = Index([10, 10, 2, 3, 4], name="test")
370-
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)