Skip to content

REF: remove Categorical._shallow_copy #41030

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
Apr 19, 2021
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
16 changes: 10 additions & 6 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,29 +1876,33 @@ def _sort_tuples(values: np.ndarray) -> np.ndarray:
return values[indexer]


def union_with_duplicates(lvals: np.ndarray, rvals: np.ndarray) -> np.ndarray:
def union_with_duplicates(lvals: ArrayLike, rvals: ArrayLike) -> ArrayLike:
"""
Extracts the union from lvals and rvals with respect to duplicates and nans in
both arrays.

Parameters
----------
lvals: np.ndarray
lvals: np.ndarray or ExtensionArray
left values which is ordered in front.
rvals: np.ndarray
rvals: np.ndarray or ExtensionArray
right values ordered after lvals.

Returns
-------
np.ndarray containing the unsorted union of both arrays
np.ndarray or ExtensionArray
Containing the unsorted union of both arrays.
"""
indexer = []
l_count = value_counts(lvals, dropna=False)
r_count = value_counts(rvals, dropna=False)
l_count, r_count = l_count.align(r_count, fill_value=0)
unique_array = unique(np.append(lvals, rvals))
if is_extension_array_dtype(lvals) or is_extension_array_dtype(rvals):
unique_array = pd_array(unique_array)
if not isinstance(lvals, np.ndarray):
# i.e. ExtensionArray
# Note: we only get here with lvals.dtype == rvals.dtype
# TODO: are there any cases where union won't be type/dtype preserving?
unique_array = type(lvals)._from_sequence(unique_array, dtype=lvals.dtype)
for i, value in enumerate(unique_array):
indexer += [i] * int(max(l_count[value], r_count[value]))
return unique_array.take(indexer)
11 changes: 2 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2982,12 +2982,7 @@ def _union(self, other: Index, sort):

elif not other.is_unique:
# other has duplicates

# error: Argument 1 to "union_with_duplicates" has incompatible type
# "Union[ExtensionArray, ndarray]"; expected "ndarray"
# error: Argument 2 to "union_with_duplicates" has incompatible type
# "Union[ExtensionArray, ndarray]"; expected "ndarray"
result = algos.union_with_duplicates(lvals, rvals) # type: ignore[arg-type]
result = algos.union_with_duplicates(lvals, rvals)
return _maybe_try_sort(result, sort)

# Self may have duplicates
Expand All @@ -3002,9 +2997,7 @@ def _union(self, other: Index, sort):
other_diff = rvals.take(missing)
result = concat_compat((lvals, other_diff))
else:
# error: Incompatible types in assignment (expression has type
# "Union[ExtensionArray, ndarray]", variable has type "ndarray")
result = lvals # type: ignore[assignment]
result = lvals

if not self.is_monotonic or not other.is_monotonic:
result = _maybe_try_sort(result, sort)
Expand Down
17 changes: 0 additions & 17 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pandas._config import get_option

from pandas._libs import index as libindex
from pandas._libs.lib import no_default
from pandas._typing import (
ArrayLike,
Dtype,
Expand Down Expand Up @@ -234,22 +233,6 @@ def __new__(

# --------------------------------------------------------------------

@doc(Index._shallow_copy)
def _shallow_copy(
self,
values: Categorical,
name: Hashable = no_default,
) -> CategoricalIndex:
name = self._name if name is no_default else name

if values is not None:
# In tests we only get here with Categorical objects that
# have matching .ordered, and values.categories a subset of
# our own. However we do _not_ have a dtype match in general.
values = Categorical(values, dtype=self.dtype)

return super()._shallow_copy(values=values, name=name)

def _is_dtype_compat(self, other) -> Categorical:
"""
*this is an internal non-public method*
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _get_unique_index(self):
return self

result = self._data.unique()
return self._shallow_copy(result)
return type(self)._simple_new(result, name=self.name)

@doc(Index.map)
def map(self, mapper, na_action=None):
Expand Down