Skip to content

BUG: MultiIndex.join losing dtype #49877

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 11 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
BUG: MultiIndex.join losing dtype
  • Loading branch information
phofl committed Nov 23, 2022
commit f6d0a799f418eb84a7d2817ba541b96b9852bd76
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ MultiIndex
- Bug in :meth:`MultiIndex.union` not sorting when sort=None and index contains missing values (:issue:`49010`)
- Bug in :meth:`MultiIndex.append` not checking names for equality (:issue:`48288`)
- Bug in :meth:`MultiIndex.symmetric_difference` losing extension array (:issue:`48607`)
- Bug in :meth:`MultiIndex.join` losing dtypes when :class:`MultiIndex` has duplicates and is non-monotonic (:issue:`49830`)
- Bug in :meth:`MultiIndex.putmask` losing extension array (:issue:`49830`)
- Bug in :meth:`MultiIndex.value_counts` returning a :class:`Series` indexed by flat index of tuples instead of a :class:`MultiIndex` (:issue:`49558`)
-
Expand Down
19 changes: 3 additions & 16 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4587,22 +4587,9 @@ def _join_non_unique(
)
mask = left_idx == -1

join_array = self._values.take(left_idx)
right = other._values.take(right_idx)

if isinstance(join_array, np.ndarray):
# error: Argument 3 to "putmask" has incompatible type
# "Union[ExtensionArray, ndarray[Any, Any]]"; expected
# "Union[_SupportsArray[dtype[Any]], _NestedSequence[
# _SupportsArray[dtype[Any]]], bool, int, float, complex,
# str, bytes, _NestedSequence[Union[bool, int, float,
# complex, str, bytes]]]"
np.putmask(join_array, mask, right) # type: ignore[arg-type]
else:
join_array._putmask(mask, right)

join_index = self._wrap_joined_index(join_array, other)

join_idx = self.take(left_idx)
right = other.take(right_idx)
join_index = join_idx.putmask(mask, right)
return join_index, left_idx, right_idx

@final
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,34 @@ def test_join_multi_with_nan():
index=MultiIndex.from_product([["A"], [1.0, 2.0]], names=["id1", "id2"]),
)
tm.assert_frame_equal(result, expected)


def test_join_dtypes(any_numeric_ea_dtype):
# GH#49830
midx = MultiIndex.from_arrays([Series([1, 2], dtype=any_numeric_ea_dtype), [3, 4]])
midx2 = MultiIndex.from_arrays(
[Series([1, 0, 0], dtype=any_numeric_ea_dtype), [3, 4, 4]]
)
result = midx.join(midx2, how="outer")
expected = MultiIndex.from_arrays(
[Series([0, 0, 1, 2], dtype=any_numeric_ea_dtype), [4, 4, 3, 4]]
)
tm.assert_index_equal(result, expected)


def test_join_dtypes_all_nan(any_numeric_ea_dtype):
# GH#49830
midx = MultiIndex.from_arrays(
[Series([1, 2], dtype=any_numeric_ea_dtype), [np.nan, np.nan]]
)
midx2 = MultiIndex.from_arrays(
[Series([1, 0, 0], dtype=any_numeric_ea_dtype), [np.nan, np.nan, np.nan]]
)
result = midx.join(midx2, how="outer")
expected = MultiIndex.from_arrays(
[
Series([0, 0, 1, 2], dtype=any_numeric_ea_dtype),
[np.nan, np.nan, np.nan, np.nan],
]
)
tm.assert_index_equal(result, expected)