Skip to content

BUG: MultiIndex.putmask #43219

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 5 commits into from
Aug 30, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ MultiIndex
- Bug in :meth:`MultiIndex.get_loc` where the first level is a :class:`DatetimeIndex` and a string key is passed (:issue:`42465`)
- Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`)
- Bug in :meth:`MultiIndex.get_loc` raising ``TypeError`` instead of ``KeyError`` on nested tuple (:issue:`42440`)
- Bug in :meth:`MultiIndex.putmask` where the other value was also a :class:`MultiIndex` (:issue:`43212`)
-

I/O
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3678,7 +3678,12 @@ def astype(self, dtype, copy: bool = True):
return self

def _validate_fill_value(self, item):
if not isinstance(item, tuple):
if isinstance(item, MultiIndex):
# GH#43212
if item.nlevels != self.nlevels:
raise ValueError("Item must have length equal to number of levels.")
return item._values
elif not isinstance(item, tuple):
# Pad the key with empty strings if lower levels of the key
# aren't specified:
item = (item,) + ("",) * (self.nlevels - 1)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/multi/test_putmask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np

from pandas import MultiIndex
import pandas._testing as tm


def test_putmask_multiindex_other():
# GH#43212 `value` is also a MultiIndex

left = MultiIndex.from_tuples([(np.nan, 6), (np.nan, 6), ("a", 4)])
right = MultiIndex.from_tuples([("a", 1), ("a", 1), ("d", 1)])
mask = np.array([True, True, False])

result = left.putmask(mask, right)

expected = MultiIndex.from_tuples([right[0], right[1], left[2]])
tm.assert_index_equal(result, expected)