Skip to content
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
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 @@ -1939,7 +1939,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 @@ -1032,3 +1032,16 @@ def test_map(self):

dict_map = {}
tm.assert_index_equal(index.map(dict_map), nan_index)

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)