Skip to content

REF: move _wrap_joined_index up to NDarrayBackedExtensionIndex #37560

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 1 commit into from
Nov 2, 2020
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
8 changes: 0 additions & 8 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name
from pandas.core.indexes.extension import NDArrayBackedExtensionIndex, inherit_names
import pandas.core.missing as missing
from pandas.core.ops import get_op_result_name

_index_doc_kwargs = dict(ibase._index_doc_kwargs)
_index_doc_kwargs.update(dict(target_klass="CategoricalIndex"))
Expand Down Expand Up @@ -669,10 +668,3 @@ def _delegate_method(self, name: str, *args, **kwargs):
if is_scalar(res):
return res
return CategoricalIndex(res, name=self.name)

def _wrap_joined_index(
self, joined: np.ndarray, other: "CategoricalIndex"
) -> "CategoricalIndex":
name = get_op_result_name(self, other)
cat = self._data._from_backing_data(joined)
return type(self)._simple_new(cat, name=name)
18 changes: 10 additions & 8 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
make_wrapped_arith_op,
)
from pandas.core.indexes.numeric import Int64Index
from pandas.core.ops import get_op_result_name
from pandas.core.tools.timedeltas import to_timedelta

if TYPE_CHECKING:
Expand Down Expand Up @@ -629,20 +628,23 @@ def insert(self, loc: int, item):
def _can_union_without_object_cast(self, other) -> bool:
return is_dtype_equal(self.dtype, other.dtype)

def _wrap_joined_index(self, joined: np.ndarray, other):
assert other.dtype == self.dtype, (other.dtype, self.dtype)
name = get_op_result_name(self, other)

def _get_join_freq(self, other):
"""
Get the freq to attach to the result of a join operation.
"""
if is_period_dtype(self.dtype):
freq = self.freq
else:
self = cast(DatetimeTimedeltaMixin, self)
freq = self.freq if self._can_fast_union(other) else None
return freq

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

return type(self)._simple_new(new_data, name=name)
result = super()._wrap_joined_index(joined, other)
result._data._freq = self._get_join_freq(other)
return result

@doc(Index._convert_arr_indexer)
def _convert_arr_indexer(self, keyarr):
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Shared methods for Index subclasses backed by ExtensionArray.
"""
from typing import List
from typing import List, TypeVar

import numpy as np

Expand All @@ -18,6 +18,8 @@
from pandas.core.indexes.base import Index
from pandas.core.ops import get_op_result_name

_T = TypeVar("_T", bound="NDArrayBackedExtensionIndex")


def inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
"""
Expand Down Expand Up @@ -338,3 +340,8 @@ def putmask(self, mask, value):
np.putmask(new_values, mask, value)
new_arr = self._data._from_backing_data(new_values)
return type(self)._simple_new(new_arr, name=self.name)

def _wrap_joined_index(self: _T, joined: np.ndarray, other: _T) -> _T:
name = get_op_result_name(self, other)
arr = self._data._from_backing_data(joined)
return type(self)._simple_new(arr, name=name)