From dbd59ac89456bc1f65322478472cac2fe45d6715 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 16:43:07 -0800 Subject: [PATCH 1/2] BUG: iloc setitem with 3d indexer not raising --- pandas/core/internals/blocks.py | 3 ++ pandas/tests/indexing/test_indexing.py | 46 +++++++++----------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 85a26179276f5..536aa53c95fba 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -830,6 +830,9 @@ def setitem(self, indexer, value): """ transpose = self.ndim == 2 + if isinstance(indexer, np.ndarray) and indexer.ndim > self.ndim: + raise ValueError(f"Cannot set values with ndim > {self.ndim}") + # coerce None values, if appropriate if value is None: if self.is_numeric: diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 98940b64330b4..476eb316471de 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -133,38 +133,24 @@ def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id): idxr = idxr(obj) nd3 = np.random.randint(5, size=(2, 2, 2)) - msg = ( - r"Buffer has wrong number of dimensions \(expected 1, " - r"got 3\)|" - "'pandas._libs.interval.IntervalTree' object has no attribute " - "'get_loc'|" # AttributeError - "unhashable type: 'numpy.ndarray'|" # TypeError - "No matching signature found|" # TypeError - r"^\[\[\[|" # pandas.core.indexing.IndexingError - "Index data must be 1-dimensional" - ) - - if (idxr_id == "iloc") or ( - ( - isinstance(obj, Series) - and idxr_id == "setitem" - and index.inferred_type - in [ - "floating", - "string", - "datetime64", - "period", - "timedelta64", - "boolean", - "categorical", - ] - ) + if idxr_id == "iloc": + err = ValueError + msg = f"Cannot set values with ndim > {obj.ndim}" + elif ( + isinstance(index, pd.IntervalIndex) + and idxr_id == "setitem" + and obj.ndim == 1 ): - idxr[nd3] = 0 + err = AttributeError + msg = ( + "'pandas._libs.interval.IntervalTree' object has no attribute 'get_loc'" + ) else: - err = (ValueError, AttributeError) - with pytest.raises(err, match=msg): - idxr[nd3] = 0 + err = ValueError + msg = r"Buffer has wrong number of dimensions \(expected 1, got 3\)|" + + with pytest.raises(err, match=msg): + idxr[nd3] = 0 def test_inf_upcast(self): # GH 16957 From 2c6e3905c417d140e8c97f6e9f090a4e86ad9991 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 19:25:20 -0800 Subject: [PATCH 2/2] fix stata tests --- pandas/io/stata.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 06bf906be7093..d651fe9f67773 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -1678,6 +1678,10 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra missing_value = StataMissingValue(um) loc = missing_loc[umissing_loc == j] + if loc.ndim == 2 and loc.shape[1] == 1: + # GH#31813 avoid trying to set Series values with wrong + # dimension + loc = loc[:, 0] replacement.iloc[loc] = missing_value else: # All replacements are identical dtype = series.dtype