Skip to content

REF: shallow_copy->simple_new/rename #39444

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 3 commits into from
Jan 28, 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
14 changes: 9 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
taken = algos.take(
self._values, indices, allow_fill=allow_fill, fill_value=self._na_value
)
return self._shallow_copy(taken)
return type(self)._simple_new(taken, name=self.name)

@final
def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
Expand Down Expand Up @@ -893,7 +893,9 @@ def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
def repeat(self, repeats, axis=None):
repeats = ensure_platform_int(repeats)
nv.validate_repeat((), {"axis": axis})
return self._shallow_copy(self._values.repeat(repeats))
res_values = self._values.repeat(repeats)

return type(self)._simple_new(res_values, name=self.name)

# --------------------------------------------------------------------
# Copying Methods
Expand Down Expand Up @@ -2463,7 +2465,8 @@ def dropna(self, how="any"):
raise ValueError(f"invalid how option: {how}")

if self.hasnans:
return self._shallow_copy(self._values[~self._isnan])
res_values = self._values[~self._isnan]
return type(self)._simple_new(res_values, name=self.name)
return self._shallow_copy()

# --------------------------------------------------------------------
Expand Down Expand Up @@ -4556,7 +4559,7 @@ def putmask(self, mask, value):
return self.astype(object).putmask(mask, value)

np.putmask(values, mask, converted)
return self._shallow_copy(values)
return type(self)._simple_new(values, name=self.name)

def equals(self, other: Any) -> bool:
"""
Expand Down Expand Up @@ -5718,7 +5721,8 @@ def delete(self, loc):
>>> idx.delete([0, 2])
Index(['b'], dtype='object')
"""
return self._shallow_copy(np.delete(self._data, loc))
res_values = np.delete(self._data, loc)
return type(self)._simple_new(res_values, name=self.name)

def insert(self, loc: int, item):
"""
Expand Down
28 changes: 26 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas._typing import Dtype
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly, doc
from pandas.util._exceptions import rewrite_exception

from pandas.core.dtypes.common import (
ensure_platform_int,
Expand Down Expand Up @@ -169,9 +170,16 @@ def _data(self):
return np.arange(self.start, self.stop, self.step, dtype=np.int64)

@cache_readonly
def _int64index(self) -> Int64Index:
def _cached_int64index(self) -> Int64Index:
return Int64Index._simple_new(self._data, name=self.name)

@property
def _int64index(self):
# wrap _cached_int64index so we can be sure its name matches self.name
res = self._cached_int64index
res._name = self._name
return res

def _get_data_as_items(self):
""" return a list of tuples of start, stop, step """
rng = self._range
Expand Down Expand Up @@ -390,6 +398,22 @@ def _get_indexer(self, target, method=None, limit=None, tolerance=None):

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

def repeat(self, repeats, axis=None):
return self._int64index.repeat(repeats, axis=axis)

def delete(self, loc):
return self._int64index.delete(loc)

def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
with rewrite_exception("Int64Index", type(self).__name__):
return self._int64index.take(
indices,
axis=axis,
allow_fill=allow_fill,
fill_value=fill_value,
**kwargs,
)

def tolist(self):
return list(self._range)

Expand Down Expand Up @@ -645,7 +669,7 @@ def _difference(self, other, sort=None):
overlap = overlap[::-1]

if len(overlap) == 0:
return self._shallow_copy(name=res_name)
return self.rename(name=res_name)
if len(overlap) == len(self):
return self[:0].rename(res_name)
if not isinstance(overlap, RangeIndex):
Expand Down