Skip to content

Commit b69c1a2

Browse files
Licht-Tjreback
authored andcommitted
BUG: Fix Index.putmask makes stack overflow with an invalid mask (#18407)
1 parent 06518b2 commit b69c1a2

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Indexing
7373
- Bug in a boolean comparison of a ``datetime.datetime`` and a ``datetime64[ns]`` dtype Series (:issue:`17965`)
7474
- Bug where a ``MultiIndex`` with more than a million records was not raising ``AttributeError`` when trying to access a missing attribute (:issue:`18165`)
7575
- Bug in :class:`IntervalIndex` constructor when a list of intervals is passed with non-default ``closed`` (:issue:`18334`)
76-
-
76+
- Bug in ``Index.putmask`` when an invalid mask passed (:issue:`18368`)
7777
-
7878

7979
I/O

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,10 @@ def putmask(self, mask, value):
19391939
try:
19401940
np.putmask(values, mask, self._convert_for_op(value))
19411941
return self._shallow_copy(values)
1942-
except (ValueError, TypeError):
1942+
except (ValueError, TypeError) as err:
1943+
if is_object_dtype(self):
1944+
raise err
1945+
19431946
# coerces to object
19441947
return self.astype(object).putmask(mask, value)
19451948

pandas/tests/indexes/common.py

+13
Original file line numberDiff line numberDiff line change
@@ -1032,3 +1032,16 @@ def test_map(self):
10321032

10331033
dict_map = {}
10341034
tm.assert_index_equal(index.map(dict_map), nan_index)
1035+
1036+
def test_putmask_with_wrong_mask(self):
1037+
# GH18368
1038+
index = self.create_index()
1039+
1040+
with pytest.raises(ValueError):
1041+
index.putmask(np.ones(len(index) + 1, np.bool), 1)
1042+
1043+
with pytest.raises(ValueError):
1044+
index.putmask(np.ones(len(index) - 1, np.bool), 1)
1045+
1046+
with pytest.raises(ValueError):
1047+
index.putmask('foo', 1)

0 commit comments

Comments
 (0)