Skip to content

BUG/REG: multiindex nested tuples with duplicates #31358

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 1 commit into from
Jan 28, 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: 2 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,8 @@ def _maybe_to_slice(loc):
mask[loc] = True
return mask

if not isinstance(key, tuple):
if not isinstance(key, (tuple, list)):
# not including list here breaks some indexing, xref #30892
loc = self._get_level_indexer(key, level=0)
return _maybe_to_slice(loc)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
length_of_indexer,
)
from pandas.core.indexes.api import Index
from pandas.core.indexes.base import InvalidIndexError

# "null slice"
_NS = slice(None, None)
Expand Down Expand Up @@ -606,7 +607,7 @@ def _get_setitem_indexer(self, key):
if isinstance(ax, ABCMultiIndex) and self.name != "iloc":
try:
return ax.get_loc(key)
except (TypeError, KeyError):
except (TypeError, KeyError, InvalidIndexError):
# TypeError e.g. passed a bool
pass

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def test_get_loc_missing_nan():
idx.get_loc(3)
with pytest.raises(KeyError, match=r"^nan$"):
idx.get_loc(np.nan)
with pytest.raises(TypeError, match=r"'\[nan\]' is an invalid key"):
with pytest.raises(TypeError, match="unhashable type: 'list'"):
# listlike/non-hashable raises TypeError
idx.get_loc([np.nan])

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexing/multiindex/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,22 @@ def test_contains(self):
assert tx[0] in idx
assert "element_not_exit" not in idx
assert "0 day 09:30:00" in idx

def test_nested_tuples_duplicates(self):
# GH#30892

dti = pd.to_datetime(["20190101", "20190101", "20190102"])
idx = pd.Index(["a", "a", "c"])
mi = pd.MultiIndex.from_arrays([dti, idx], names=["index1", "index2"])

df = pd.DataFrame({"c1": [1, 2, 3], "c2": [np.nan, np.nan, np.nan]}, index=mi)

expected = pd.DataFrame({"c1": df["c1"], "c2": [1.0, 1.0, np.nan]}, index=mi)

df2 = df.copy(deep=True)
df2.loc[(dti[0], "a"), "c2"] = 1.0
tm.assert_frame_equal(df2, expected)

df3 = df.copy(deep=True)
df3.loc[[(dti[0], "a")], "c2"] = 1.0
tm.assert_frame_equal(df3, expected)