Skip to content

Commit 54a6490

Browse files
jbrockmendeljreback
authored andcommitted
BUG/REF: multiindex nested tuples with duplicates (#31358)
1 parent 82acdee commit 54a6490

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

pandas/core/indexes/multi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,8 @@ def _maybe_to_slice(loc):
26482648
mask[loc] = True
26492649
return mask
26502650

2651-
if not isinstance(key, tuple):
2651+
if not isinstance(key, (tuple, list)):
2652+
# not including list here breaks some indexing, xref #30892
26522653
loc = self._get_level_indexer(key, level=0)
26532654
return _maybe_to_slice(loc)
26542655

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
length_of_indexer,
2929
)
3030
from pandas.core.indexes.api import Index
31+
from pandas.core.indexes.base import InvalidIndexError
3132

3233
# "null slice"
3334
_NS = slice(None, None)
@@ -606,7 +607,7 @@ def _get_setitem_indexer(self, key):
606607
if isinstance(ax, ABCMultiIndex) and self.name != "iloc":
607608
try:
608609
return ax.get_loc(key)
609-
except (TypeError, KeyError):
610+
except (TypeError, KeyError, InvalidIndexError):
610611
# TypeError e.g. passed a bool
611612
pass
612613

pandas/tests/indexes/multi/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def test_get_loc_missing_nan():
396396
idx.get_loc(3)
397397
with pytest.raises(KeyError, match=r"^nan$"):
398398
idx.get_loc(np.nan)
399-
with pytest.raises(TypeError, match=r"'\[nan\]' is an invalid key"):
399+
with pytest.raises(TypeError, match="unhashable type: 'list'"):
400400
# listlike/non-hashable raises TypeError
401401
idx.get_loc([np.nan])
402402

pandas/tests/indexing/multiindex/test_multiindex.py

+19
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,22 @@ def test_contains(self):
9292
assert tx[0] in idx
9393
assert "element_not_exit" not in idx
9494
assert "0 day 09:30:00" in idx
95+
96+
def test_nested_tuples_duplicates(self):
97+
# GH#30892
98+
99+
dti = pd.to_datetime(["20190101", "20190101", "20190102"])
100+
idx = pd.Index(["a", "a", "c"])
101+
mi = pd.MultiIndex.from_arrays([dti, idx], names=["index1", "index2"])
102+
103+
df = pd.DataFrame({"c1": [1, 2, 3], "c2": [np.nan, np.nan, np.nan]}, index=mi)
104+
105+
expected = pd.DataFrame({"c1": df["c1"], "c2": [1.0, 1.0, np.nan]}, index=mi)
106+
107+
df2 = df.copy(deep=True)
108+
df2.loc[(dti[0], "a"), "c2"] = 1.0
109+
tm.assert_frame_equal(df2, expected)
110+
111+
df3 = df.copy(deep=True)
112+
df3.loc[[(dti[0], "a")], "c2"] = 1.0
113+
tm.assert_frame_equal(df3, expected)

0 commit comments

Comments
 (0)