From 7693aea2ae5bd18390d1c12529bc9021a48fd047 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 27 Jan 2021 17:32:50 -0800 Subject: [PATCH 1/2] REF: shallow_copy->simple_new/rename --- pandas/core/indexes/base.py | 14 +++++++++----- pandas/core/indexes/range.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 40215ea87f978..d00635b4d1551 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -824,7 +824,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: @@ -892,7 +892,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 @@ -2462,7 +2464,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() # -------------------------------------------------------------------- @@ -4553,7 +4556,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: """ @@ -5715,7 +5718,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): """ diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index fbc7f578d760e..6453f66815f27 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -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, @@ -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 __int64index(self) -> Int64Index: return Int64Index._simple_new(self._data, name=self.name) + @property + def _int64index(self): + # wrap __int64index so we can be sure its name matches self.name + res = self.__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 @@ -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) @@ -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): From 82d00446864610bda87862a9c9479f4c5cb7e036 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 27 Jan 2021 18:49:17 -0800 Subject: [PATCH 2/2] __int64index -> _cached_int64index --- pandas/core/indexes/range.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 6453f66815f27..f65102dbaa611 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -170,13 +170,13 @@ 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 __int64index so we can be sure its name matches self.name - res = self.__int64index + # wrap _cached_int64index so we can be sure its name matches self.name + res = self._cached_int64index res._name = self._name return res