From 2243252a8aef1a7939ec406eafc9599882b8b7cf Mon Sep 17 00:00:00 2001 From: Licht-T Date: Tue, 21 Nov 2017 21:29:38 +0900 Subject: [PATCH 1/3] BUG: Fix Index.putmask not to make stack overflow with an invalid mask --- pandas/core/indexes/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 57454e6fce118..f7bb8aefb5fb3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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) From 86280fec888c2b170fe52b31c64cf83aff6fb6ad Mon Sep 17 00:00:00 2001 From: Licht-T Date: Tue, 21 Nov 2017 21:31:15 +0900 Subject: [PATCH 2/3] TST: Add tests for Index.putmask with an invalid mask --- pandas/tests/indexes/common.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 456e5a9bd6439..3a57337efea6f 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -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) From 0b71ffee56264170bacf500986cb822fb0fb6e73 Mon Sep 17 00:00:00 2001 From: Licht-T Date: Sat, 25 Nov 2017 22:57:01 +0900 Subject: [PATCH 3/3] DOC: Add whatsnew note --- doc/source/whatsnew/v0.21.1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.1.txt b/doc/source/whatsnew/v0.21.1.txt index 0ab536f2898c7..ba77a1678f5b5 100644 --- a/doc/source/whatsnew/v0.21.1.txt +++ b/doc/source/whatsnew/v0.21.1.txt @@ -75,7 +75,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 ``Index.putmask`` when an invalid mask passed (:issue:`18368`) - I/O