Skip to content

REF: ensure CategoricalIndex._shallow_copy only ever gets Categorical #37917

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 18, 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
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2734,14 +2734,16 @@ def _union(self, other, sort):
stacklevel=3,
)

return self._shallow_copy(result)
return result

@final
def _wrap_setop_result(self, other, result):
if isinstance(self, (ABCDatetimeIndex, ABCTimedeltaIndex)) and isinstance(
result, np.ndarray
):
result = type(self._data)._simple_new(result, dtype=self.dtype)
elif is_categorical_dtype(self.dtype) and isinstance(result, np.ndarray):
result = Categorical(result, dtype=self.dtype)

name = get_op_result_name(self, other)
if isinstance(result, Index):
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List
from typing import Any, List, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -227,10 +227,15 @@ def _simple_new(cls, values: Categorical, name: Label = None):
# --------------------------------------------------------------------

@doc(Index._shallow_copy)
def _shallow_copy(self, values=None, name: Label = no_default):
def _shallow_copy(
self, values: Optional[Categorical] = None, name: Label = no_default
):
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)
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,7 @@ def intersection(self, other, sort=False):
start = right[0]

if end < start:
# pandas\core\indexes\datetimelike.py:758: error: Unexpected
# keyword argument "freq" for "DatetimeTimedeltaMixin" [call-arg]
result = type(self)(
data=[], dtype=self.dtype, freq=self.freq # type: ignore[call-arg]
)
result = self[:0]
else:
lslice = slice(*left.slice_locs(start, end))
left_chunk = left._values[lslice]
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from pandas.core.construction import extract_array
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import _index_shared_docs, maybe_extract_name
from pandas.core.indexes.numeric import Int64Index
from pandas.core.indexes.numeric import Float64Index, Int64Index
from pandas.core.ops.common import unpack_zerodim_and_defer

_empty_range = range(0)
Expand Down Expand Up @@ -397,6 +397,8 @@ def _shallow_copy(self, values=None, name: Label = no_default):
name = self.name if name is no_default else name

if values is not None:
if values.dtype.kind == "f":
return Float64Index(values, name=name)
return Int64Index._simple_new(values, name=name)

result = self._simple_new(self._range, name=name)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/base_class/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def test_setops_preserve_object_dtype(self):

result = idx._union(idx[1:], sort=None)
expected = idx
tm.assert_index_equal(result, expected)
tm.assert_numpy_array_equal(result, expected.values)

result = idx.union(idx[1:], sort=None)
tm.assert_index_equal(result, expected)

# if other is not monotonic increasing, _union goes through
# a different route
result = idx._union(idx[1:][::-1], sort=None)
tm.assert_index_equal(result, expected)
tm.assert_numpy_array_equal(result, expected.values)

result = idx.union(idx[1:][::-1], sort=None)
tm.assert_index_equal(result, expected)
Expand Down