Skip to content

Commit 34926ff

Browse files
authored
BUG: MultiIndex.putmask (#43219)
1 parent 9cb969e commit 34926ff

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ MultiIndex
325325
- Bug in :meth:`MultiIndex.get_loc` where the first level is a :class:`DatetimeIndex` and a string key is passed (:issue:`42465`)
326326
- Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`)
327327
- Bug in :meth:`MultiIndex.get_loc` raising ``TypeError`` instead of ``KeyError`` on nested tuple (:issue:`42440`)
328+
- Bug in :meth:`MultiIndex.putmask` where the other value was also a :class:`MultiIndex` (:issue:`43212`)
328329
-
329330

330331
I/O

pandas/core/indexes/multi.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3683,7 +3683,12 @@ def astype(self, dtype, copy: bool = True):
36833683
return self
36843684

36853685
def _validate_fill_value(self, item):
3686-
if not isinstance(item, tuple):
3686+
if isinstance(item, MultiIndex):
3687+
# GH#43212
3688+
if item.nlevels != self.nlevels:
3689+
raise ValueError("Item must have length equal to number of levels.")
3690+
return item._values
3691+
elif not isinstance(item, tuple):
36873692
# Pad the key with empty strings if lower levels of the key
36883693
# aren't specified:
36893694
item = (item,) + ("",) * (self.nlevels - 1)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
from pandas import MultiIndex
4+
import pandas._testing as tm
5+
6+
7+
def test_putmask_multiindex_other():
8+
# GH#43212 `value` is also a MultiIndex
9+
10+
left = MultiIndex.from_tuples([(np.nan, 6), (np.nan, 6), ("a", 4)])
11+
right = MultiIndex.from_tuples([("a", 1), ("a", 1), ("d", 1)])
12+
mask = np.array([True, True, False])
13+
14+
result = left.putmask(mask, right)
15+
16+
expected = MultiIndex.from_tuples([right[0], right[1], left[2]])
17+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)