Skip to content

Commit 9c30c07

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF: de-duplicate _wrap_joined_index in MultiIndex (pandas-dev#36313)
1 parent eb38bb7 commit 9c30c07

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3876,7 +3876,10 @@ def _join_monotonic(self, other, how="left", return_indexers=False):
38763876
return join_index
38773877

38783878
def _wrap_joined_index(self, joined, other):
3879-
name = get_op_result_name(self, other)
3879+
if isinstance(self, ABCMultiIndex):
3880+
name = self.names if self.names == other.names else None
3881+
else:
3882+
name = get_op_result_name(self, other)
38803883
return self._constructor(joined, name=name)
38813884

38823885
# --------------------------------------------------------------------

pandas/core/indexes/multi.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import wraps
12
from sys import getsizeof
23
from typing import (
34
TYPE_CHECKING,
@@ -152,6 +153,25 @@ def _codes_to_ints(self, codes):
152153
return np.bitwise_or.reduce(codes, axis=1)
153154

154155

156+
def names_compat(meth):
157+
"""
158+
A decorator to allow either `name` or `names` keyword but not both.
159+
160+
This makes it easier to share code with base class.
161+
"""
162+
163+
@wraps(meth)
164+
def new_meth(self_or_cls, *args, **kwargs):
165+
if "name" in kwargs and "names" in kwargs:
166+
raise TypeError("Can only provide one of `names` and `name`")
167+
elif "name" in kwargs:
168+
kwargs["names"] = kwargs.pop("name")
169+
170+
return meth(self_or_cls, *args, **kwargs)
171+
172+
return new_meth
173+
174+
155175
class MultiIndex(Index):
156176
"""
157177
A multi-level, or hierarchical, index object for pandas objects.
@@ -449,6 +469,7 @@ def from_arrays(cls, arrays, sortorder=None, names=lib.no_default) -> "MultiInde
449469
)
450470

451471
@classmethod
472+
@names_compat
452473
def from_tuples(
453474
cls,
454475
tuples,
@@ -3635,10 +3656,6 @@ def delete(self, loc):
36353656
verify_integrity=False,
36363657
)
36373658

3638-
def _wrap_joined_index(self, joined, other):
3639-
names = self.names if self.names == other.names else None
3640-
return self._constructor(joined, names=names)
3641-
36423659
@doc(Index.isin)
36433660
def isin(self, values, level=None):
36443661
if level is None:

0 commit comments

Comments
 (0)