Skip to content

Commit c836828

Browse files
jbrockmendelTomAugspurger
authored andcommitted
"Backport PR #31358 on branch 1.0.x" (#31376)
* BUG/REF: multiindex nested tuples with duplicates (#31358)
1 parent 6965a5b commit c836828

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

pandas/core/indexes/multi.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,7 @@ def _partial_tup_index(self, tup, side="left"):
25462546
for k, (lab, lev, labs) in enumerate(zipped):
25472547
section = labs[start:end]
25482548

2549-
if lab not in lev and not isna(lab):
2549+
if lab not in lev and np.ndim(lab) == 0 and not isna(lab):
25502550
if not lev.is_type_compatible(lib.infer_dtype([lab], skipna=False)):
25512551
raise TypeError(f"Level type mismatch: {lab}")
25522552

@@ -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

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def _get_setitem_indexer(self, key):
639639
if isinstance(ax, ABCMultiIndex) and self.name != "iloc":
640640
try:
641641
return ax.get_loc(key)
642-
except (TypeError, KeyError):
642+
except (TypeError, KeyError, InvalidIndexError):
643643
# TypeError e.g. passed a bool
644644
pass
645645

pandas/tests/indexes/multi/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ 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(KeyError, match=r"^\[nan\]$"):
399+
with pytest.raises(TypeError, match="unhashable type: 'list'"):
400+
# listlike/non-hashable raises TypeError
400401
idx.get_loc([np.nan])
401402

402403

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)