Skip to content

Commit 2be78cc

Browse files
authored
REF: shallow_copy->simple_new/rename (#39444)
1 parent 624621b commit 2be78cc

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

pandas/core/indexes/base.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
825825
taken = algos.take(
826826
self._values, indices, allow_fill=allow_fill, fill_value=self._na_value
827827
)
828-
return self._shallow_copy(taken)
828+
return type(self)._simple_new(taken, name=self.name)
829829

830830
@final
831831
def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
@@ -893,7 +893,9 @@ def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
893893
def repeat(self, repeats, axis=None):
894894
repeats = ensure_platform_int(repeats)
895895
nv.validate_repeat((), {"axis": axis})
896-
return self._shallow_copy(self._values.repeat(repeats))
896+
res_values = self._values.repeat(repeats)
897+
898+
return type(self)._simple_new(res_values, name=self.name)
897899

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

24652467
if self.hasnans:
2466-
return self._shallow_copy(self._values[~self._isnan])
2468+
res_values = self._values[~self._isnan]
2469+
return type(self)._simple_new(res_values, name=self.name)
24672470
return self._shallow_copy()
24682471

24692472
# --------------------------------------------------------------------
@@ -4556,7 +4559,7 @@ def putmask(self, mask, value):
45564559
return self.astype(object).putmask(mask, value)
45574560

45584561
np.putmask(values, mask, converted)
4559-
return self._shallow_copy(values)
4562+
return type(self)._simple_new(values, name=self.name)
45604563

45614564
def equals(self, other: Any) -> bool:
45624565
"""
@@ -5718,7 +5721,8 @@ def delete(self, loc):
57185721
>>> idx.delete([0, 2])
57195722
Index(['b'], dtype='object')
57205723
"""
5721-
return self._shallow_copy(np.delete(self._data, loc))
5724+
res_values = np.delete(self._data, loc)
5725+
return type(self)._simple_new(res_values, name=self.name)
57225726

57235727
def insert(self, loc: int, item):
57245728
"""

pandas/core/indexes/range.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas._typing import Dtype
1414
from pandas.compat.numpy import function as nv
1515
from pandas.util._decorators import cache_readonly, doc
16+
from pandas.util._exceptions import rewrite_exception
1617

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

171172
@cache_readonly
172-
def _int64index(self) -> Int64Index:
173+
def _cached_int64index(self) -> Int64Index:
173174
return Int64Index._simple_new(self._data, name=self.name)
174175

176+
@property
177+
def _int64index(self):
178+
# wrap _cached_int64index so we can be sure its name matches self.name
179+
res = self._cached_int64index
180+
res._name = self._name
181+
return res
182+
175183
def _get_data_as_items(self):
176184
""" return a list of tuples of start, stop, step """
177185
rng = self._range
@@ -390,6 +398,22 @@ def _get_indexer(self, target, method=None, limit=None, tolerance=None):
390398

391399
# --------------------------------------------------------------------
392400

401+
def repeat(self, repeats, axis=None):
402+
return self._int64index.repeat(repeats, axis=axis)
403+
404+
def delete(self, loc):
405+
return self._int64index.delete(loc)
406+
407+
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
408+
with rewrite_exception("Int64Index", type(self).__name__):
409+
return self._int64index.take(
410+
indices,
411+
axis=axis,
412+
allow_fill=allow_fill,
413+
fill_value=fill_value,
414+
**kwargs,
415+
)
416+
393417
def tolist(self):
394418
return list(self._range)
395419

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

647671
if len(overlap) == 0:
648-
return self._shallow_copy(name=res_name)
672+
return self.rename(name=res_name)
649673
if len(overlap) == len(self):
650674
return self[:0].rename(res_name)
651675
if not isinstance(overlap, RangeIndex):

0 commit comments

Comments
 (0)