Skip to content

Commit

Permalink
BUG: Fix Index.putmask makes stack overflow with an invalid mask (#18407
Browse files Browse the repository at this point in the history
)

(cherry picked from commit b69c1a2)
  • Loading branch information
Licht-T authored and TomAugspurger committed Dec 11, 2017
1 parent e2ba360 commit 0663d76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Indexing
- Bug in a boolean comparison of a ``datetime.datetime`` and a ``datetime64[ns]`` dtype Series (:issue:`17965`)
- Bug where a ``MultiIndex`` with more than a million records was not raising ``AttributeError`` when trying to access a missing attribute (:issue:`18165`)
- Bug in :class:`IntervalIndex` constructor when a list of intervals is passed with non-default ``closed`` (:issue:`18334`)
-
- Bug in ``Index.putmask`` when an invalid mask passed (:issue:`18368`)
-

I/O
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,10 @@ def putmask(self, mask, value):
try:
np.putmask(values, mask, self._convert_for_op(value))
return self._shallow_copy(values)
except (ValueError, TypeError):
except (ValueError, TypeError) as err:
if is_object_dtype(self):
raise err

# coerces to object
return self.astype(object).putmask(mask, value)

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,3 +996,16 @@ def test_searchsorted_monotonic(self, indices):
# non-monotonic should raise.
with pytest.raises(ValueError):
indices._searchsorted_monotonic(value, side='left')

def test_putmask_with_wrong_mask(self):
# GH18368
index = self.create_index()

with pytest.raises(ValueError):
index.putmask(np.ones(len(index) + 1, np.bool), 1)

with pytest.raises(ValueError):
index.putmask(np.ones(len(index) - 1, np.bool), 1)

with pytest.raises(ValueError):
index.putmask('foo', 1)

0 comments on commit 0663d76

Please sign in to comment.