Skip to content

Commit 347a7bc

Browse files
authored
CLN: make MultiIndex._shallow_copy signature match other subclasses (#37985)
1 parent e640dde commit 347a7bc

File tree

6 files changed

+69
-74
lines changed

6 files changed

+69
-74
lines changed

pandas/core/indexes/base.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -3986,7 +3986,11 @@ def _join_monotonic(self, other, how="left", return_indexers=False):
39863986
else:
39873987
return join_index
39883988

3989-
def _wrap_joined_index(self, joined, other):
3989+
def _wrap_joined_index(
3990+
self: _IndexT, joined: np.ndarray, other: _IndexT
3991+
) -> _IndexT:
3992+
assert other.dtype == self.dtype
3993+
39903994
if isinstance(self, ABCMultiIndex):
39913995
name = self.names if self.names == other.names else None
39923996
else:
@@ -4188,7 +4192,7 @@ def _is_memory_usage_qualified(self) -> bool:
41884192
"""
41894193
return self.is_object()
41904194

4191-
def is_type_compatible(self, kind) -> bool:
4195+
def is_type_compatible(self, kind: str_t) -> bool:
41924196
"""
41934197
Whether the index type is compatible with the provided type.
41944198
"""

pandas/core/indexes/datetimelike.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,36 @@ def _format_attrs(self):
403403
attrs.append(("freq", freq))
404404
return attrs
405405

406+
def _summary(self, name=None) -> str:
407+
"""
408+
Return a summarized representation.
409+
410+
Parameters
411+
----------
412+
name : str
413+
Name to use in the summary representation.
414+
415+
Returns
416+
-------
417+
str
418+
Summarized representation of the index.
419+
"""
420+
formatter = self._formatter_func
421+
if len(self) > 0:
422+
index_summary = f", {formatter(self[0])} to {formatter(self[-1])}"
423+
else:
424+
index_summary = ""
425+
426+
if name is None:
427+
name = type(self).__name__
428+
result = f"{name}: {len(self)} entries{index_summary}"
429+
if self.freq:
430+
result += f"\nFreq: {self.freqstr}"
431+
432+
# display as values, not quoted
433+
result = result.replace("'", "")
434+
return result
435+
406436
# --------------------------------------------------------------------
407437
# Indexing Methods
408438

@@ -507,36 +537,6 @@ def where(self, cond, other=None):
507537
arr = self._data._from_backing_data(result)
508538
return type(self)._simple_new(arr, name=self.name)
509539

510-
def _summary(self, name=None) -> str:
511-
"""
512-
Return a summarized representation.
513-
514-
Parameters
515-
----------
516-
name : str
517-
Name to use in the summary representation.
518-
519-
Returns
520-
-------
521-
str
522-
Summarized representation of the index.
523-
"""
524-
formatter = self._formatter_func
525-
if len(self) > 0:
526-
index_summary = f", {formatter(self[0])} to {formatter(self[-1])}"
527-
else:
528-
index_summary = ""
529-
530-
if name is None:
531-
name = type(self).__name__
532-
result = f"{name}: {len(self)} entries{index_summary}"
533-
if self.freq:
534-
result += f"\nFreq: {self.freqstr}"
535-
536-
# display as values, not quoted
537-
result = result.replace("'", "")
538-
return result
539-
540540
def shift(self, periods=1, freq=None):
541541
"""
542542
Shift index by desired number of time frequency increments.

pandas/core/indexes/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
814814

815815
# --------------------------------------------------------------------
816816

817-
def is_type_compatible(self, typ) -> bool:
818-
return typ == self.inferred_type or typ == "datetime"
817+
def is_type_compatible(self, kind: str) -> bool:
818+
return kind == self.inferred_type or kind == "datetime"
819819

820820
@property
821821
def inferred_type(self) -> str:

pandas/core/indexes/multi.py

+28-37
Original file line numberDiff line numberDiff line change
@@ -1065,53 +1065,26 @@ def _engine(self):
10651065

10661066
@property
10671067
def _constructor(self):
1068-
return MultiIndex.from_tuples
1068+
return type(self).from_tuples
10691069

10701070
@doc(Index._shallow_copy)
1071-
def _shallow_copy(
1072-
self,
1073-
values=None,
1074-
name=lib.no_default,
1075-
levels=None,
1076-
codes=None,
1077-
sortorder=None,
1078-
names=lib.no_default,
1079-
):
1080-
if names is not lib.no_default and name is not lib.no_default:
1081-
raise TypeError("Can only provide one of `names` and `name`")
1082-
elif names is lib.no_default:
1083-
names = name if name is not lib.no_default else self.names
1071+
def _shallow_copy(self, values=None, name=lib.no_default):
1072+
names = name if name is not lib.no_default else self.names
10841073

10851074
if values is not None:
1086-
assert levels is None and codes is None
1087-
return MultiIndex.from_tuples(values, sortorder=sortorder, names=names)
1075+
return type(self).from_tuples(values, sortorder=None, names=names)
10881076

1089-
levels = levels if levels is not None else self.levels
1090-
codes = codes if codes is not None else self.codes
1091-
1092-
result = MultiIndex(
1093-
levels=levels,
1094-
codes=codes,
1095-
sortorder=sortorder,
1077+
result = type(self)(
1078+
levels=self.levels,
1079+
codes=self.codes,
1080+
sortorder=None,
10961081
names=names,
10971082
verify_integrity=False,
10981083
)
10991084
result._cache = self._cache.copy()
11001085
result._cache.pop("levels", None) # GH32669
11011086
return result
11021087

1103-
def symmetric_difference(self, other, result_name=None, sort=None):
1104-
# On equal symmetric_difference MultiIndexes the difference is empty.
1105-
# Therefore, an empty MultiIndex is returned GH13490
1106-
tups = Index.symmetric_difference(self, other, result_name, sort)
1107-
if len(tups) == 0:
1108-
return MultiIndex(
1109-
levels=[[] for _ in range(self.nlevels)],
1110-
codes=[[] for _ in range(self.nlevels)],
1111-
names=tups.name,
1112-
)
1113-
return type(self).from_tuples(tups, names=tups.name)
1114-
11151088
# --------------------------------------------------------------------
11161089

11171090
def copy(
@@ -1177,12 +1150,18 @@ def copy(
11771150
if codes is None:
11781151
codes = deepcopy(self.codes)
11791152

1180-
new_index = self._shallow_copy(
1153+
levels = levels if levels is not None else self.levels
1154+
codes = codes if codes is not None else self.codes
1155+
1156+
new_index = type(self)(
11811157
levels=levels,
11821158
codes=codes,
1183-
names=names,
11841159
sortorder=self.sortorder,
1160+
names=names,
1161+
verify_integrity=False,
11851162
)
1163+
new_index._cache = self._cache.copy()
1164+
new_index._cache.pop("levels", None) # GH32669
11861165

11871166
if dtype:
11881167
warnings.warn(
@@ -3612,6 +3591,18 @@ def _convert_can_do_setop(self, other):
36123591

36133592
return other, result_names
36143593

3594+
def symmetric_difference(self, other, result_name=None, sort=None):
3595+
# On equal symmetric_difference MultiIndexes the difference is empty.
3596+
# Therefore, an empty MultiIndex is returned GH13490
3597+
tups = Index.symmetric_difference(self, other, result_name, sort)
3598+
if len(tups) == 0:
3599+
return type(self)(
3600+
levels=[[] for _ in range(self.nlevels)],
3601+
codes=[[] for _ in range(self.nlevels)],
3602+
names=tups.name,
3603+
)
3604+
return type(self).from_tuples(tups, names=tups.name)
3605+
36153606
# --------------------------------------------------------------------
36163607

36173608
@doc(Index.astype)

pandas/core/indexes/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def __new__(
250250

251251
@property
252252
def values(self) -> np.ndarray:
253-
return np.asarray(self)
253+
return np.asarray(self, dtype=object)
254254

255255
def _maybe_convert_timedelta(self, other):
256256
"""

pandas/core/indexes/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
229229

230230
# -------------------------------------------------------------------
231231

232-
def is_type_compatible(self, typ) -> bool:
233-
return typ == self.inferred_type or typ == "timedelta"
232+
def is_type_compatible(self, kind: str) -> bool:
233+
return kind == self.inferred_type or kind == "timedelta"
234234

235235
@property
236236
def inferred_type(self) -> str:

0 commit comments

Comments
 (0)