Skip to content

Commit e959fc5

Browse files
jbrockmendelukarroum
authored andcommitted
REF: move _wrap_joined_index up to NDarrayBackedExtensionIndex (#37560)
1 parent 1b6c423 commit e959fc5

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

pandas/core/indexes/category.py

-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name
3030
from pandas.core.indexes.extension import NDArrayBackedExtensionIndex, inherit_names
3131
import pandas.core.missing as missing
32-
from pandas.core.ops import get_op_result_name
3332

3433
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3534
_index_doc_kwargs.update(dict(target_klass="CategoricalIndex"))
@@ -669,10 +668,3 @@ def _delegate_method(self, name: str, *args, **kwargs):
669668
if is_scalar(res):
670669
return res
671670
return CategoricalIndex(res, name=self.name)
672-
673-
def _wrap_joined_index(
674-
self, joined: np.ndarray, other: "CategoricalIndex"
675-
) -> "CategoricalIndex":
676-
name = get_op_result_name(self, other)
677-
cat = self._data._from_backing_data(joined)
678-
return type(self)._simple_new(cat, name=name)

pandas/core/indexes/datetimelike.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
make_wrapped_arith_op,
3939
)
4040
from pandas.core.indexes.numeric import Int64Index
41-
from pandas.core.ops import get_op_result_name
4241
from pandas.core.tools.timedeltas import to_timedelta
4342

4443
if TYPE_CHECKING:
@@ -629,20 +628,23 @@ def insert(self, loc: int, item):
629628
def _can_union_without_object_cast(self, other) -> bool:
630629
return is_dtype_equal(self.dtype, other.dtype)
631630

632-
def _wrap_joined_index(self, joined: np.ndarray, other):
633-
assert other.dtype == self.dtype, (other.dtype, self.dtype)
634-
name = get_op_result_name(self, other)
635-
631+
def _get_join_freq(self, other):
632+
"""
633+
Get the freq to attach to the result of a join operation.
634+
"""
636635
if is_period_dtype(self.dtype):
637636
freq = self.freq
638637
else:
639638
self = cast(DatetimeTimedeltaMixin, self)
640639
freq = self.freq if self._can_fast_union(other) else None
640+
return freq
641641

642-
new_data = self._data._from_backing_data(joined)
643-
new_data._freq = freq
642+
def _wrap_joined_index(self, joined: np.ndarray, other):
643+
assert other.dtype == self.dtype, (other.dtype, self.dtype)
644644

645-
return type(self)._simple_new(new_data, name=name)
645+
result = super()._wrap_joined_index(joined, other)
646+
result._data._freq = self._get_join_freq(other)
647+
return result
646648

647649
@doc(Index._convert_arr_indexer)
648650
def _convert_arr_indexer(self, keyarr):

pandas/core/indexes/extension.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Shared methods for Index subclasses backed by ExtensionArray.
33
"""
4-
from typing import List
4+
from typing import List, TypeVar
55

66
import numpy as np
77

@@ -18,6 +18,8 @@
1818
from pandas.core.indexes.base import Index
1919
from pandas.core.ops import get_op_result_name
2020

21+
_T = TypeVar("_T", bound="NDArrayBackedExtensionIndex")
22+
2123

2224
def inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
2325
"""
@@ -349,3 +351,8 @@ def putmask(self, mask, value):
349351
np.putmask(new_values, mask, value)
350352
new_arr = self._data._from_backing_data(new_values)
351353
return type(self)._simple_new(new_arr, name=self.name)
354+
355+
def _wrap_joined_index(self: _T, joined: np.ndarray, other: _T) -> _T:
356+
name = get_op_result_name(self, other)
357+
arr = self._data._from_backing_data(joined)
358+
return type(self)._simple_new(arr, name=name)

0 commit comments

Comments
 (0)