Skip to content

Commit 0663d76

Browse files
Licht-TTomAugspurger
authored andcommitted
BUG: Fix Index.putmask makes stack overflow with an invalid mask (#18407)
(cherry picked from commit b69c1a2)
1 parent e2ba360 commit 0663d76

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
@@ -1934,7 +1934,10 @@ def putmask(self, mask, value):
19341934
try:
19351935
np.putmask(values, mask, self._convert_for_op(value))
19361936
return self._shallow_copy(values)
1937-
except (ValueError, TypeError):
1937+
except (ValueError, TypeError) as err:
1938+
if is_object_dtype(self):
1939+
raise err
1940+
19381941
# coerces to object
19391942
return self.astype(object).putmask(mask, value)
19401943

pandas/tests/indexes/common.py

+13
Original file line numberDiff line numberDiff line change
@@ -996,3 +996,16 @@ def test_searchsorted_monotonic(self, indices):
996996
# non-monotonic should raise.
997997
with pytest.raises(ValueError):
998998
indices._searchsorted_monotonic(value, side='left')
999+
1000+
def test_putmask_with_wrong_mask(self):
1001+
# GH18368
1002+
index = self.create_index()
1003+
1004+
with pytest.raises(ValueError):
1005+
index.putmask(np.ones(len(index) + 1, np.bool), 1)
1006+
1007+
with pytest.raises(ValueError):
1008+
index.putmask(np.ones(len(index) - 1, np.bool), 1)
1009+
1010+
with pytest.raises(ValueError):
1011+
index.putmask('foo', 1)

0 commit comments

Comments
 (0)