Skip to content

BUG: iloc setitem with 3d indexer not raising #31813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2020
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
3 changes: 3 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make a note to refactor this; this seems a bit fishy; cc @bashtage

# 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
Expand Down
46 changes: 16 additions & 30 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down