Skip to content

Commit 73fd8a7

Browse files
authored
CLN: remove shallow_copy_with_infer (#33691)
1 parent 92354a8 commit 73fd8a7

File tree

7 files changed

+15
-56
lines changed

7 files changed

+15
-56
lines changed

pandas/core/indexes/base.py

+6-46
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ def _outer_indexer(self, left, right):
269269
# would we like our indexing holder to defer to us
270270
_defer_to_indexing = False
271271

272-
# prioritize current class for _shallow_copy_with_infer,
273-
# used to infer integers as datetime-likes
274-
_infer_as_myclass = False
275-
276272
_engine_type = libindex.ObjectEngine
277273
# whether we support partial string indexing. Overridden
278274
# in DatetimeIndex and PeriodIndex
@@ -440,10 +436,6 @@ def __new__(
440436
_simple_new), but fills caller's metadata otherwise specified. Passed
441437
kwargs will overwrite corresponding metadata.
442438
443-
- _shallow_copy_with_infer: It returns new Index inferring its type
444-
from passed values. It fills caller's metadata otherwise specified as the
445-
same as _shallow_copy.
446-
447439
See each method's docstring.
448440
"""
449441

@@ -516,35 +508,6 @@ def _shallow_copy(self, values=None, name: Label = no_default):
516508
result._cache = cache
517509
return result
518510

519-
def _shallow_copy_with_infer(self, values, **kwargs):
520-
"""
521-
Create a new Index inferring the class with passed value, don't copy
522-
the data, use the same object attributes with passed in attributes
523-
taking precedence.
524-
525-
*this is an internal non-public method*
526-
527-
Parameters
528-
----------
529-
values : the values to create the new Index, optional
530-
kwargs : updates the default attributes for this Index
531-
"""
532-
attributes = self._get_attributes_dict()
533-
attributes.update(kwargs)
534-
attributes["copy"] = False
535-
if not len(values) and "dtype" not in kwargs:
536-
# TODO: what if hasattr(values, "dtype")?
537-
attributes["dtype"] = self.dtype
538-
if self._infer_as_myclass:
539-
try:
540-
return self._constructor(values, **attributes)
541-
except (TypeError, ValueError):
542-
pass
543-
544-
# Remove tz so Index will try non-DatetimeIndex inference
545-
attributes.pop("tz", None)
546-
return Index(values, **attributes)
547-
548511
def is_(self, other) -> bool:
549512
"""
550513
More flexible, faster check like ``is`` but that works through views.
@@ -2809,11 +2772,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
28092772
except TypeError:
28102773
pass
28112774

2812-
attribs = self._get_attributes_dict()
2813-
attribs["name"] = result_name
2814-
if "freq" in attribs:
2815-
attribs["freq"] = None
2816-
return self._shallow_copy_with_infer(the_diff, **attribs)
2775+
return Index(the_diff, dtype=self.dtype, name=result_name)
28172776

28182777
def _assert_can_do_setop(self, other):
28192778
if not is_list_like(other):
@@ -3387,7 +3346,7 @@ def _reindex_non_unique(self, target):
33873346
new_indexer = np.arange(len(self.take(indexer)))
33883347
new_indexer[~check] = -1
33893348

3390-
new_index = self._shallow_copy_with_infer(new_labels)
3349+
new_index = Index(new_labels, name=self.name)
33913350
return new_index, indexer, new_indexer
33923351

33933352
# --------------------------------------------------------------------
@@ -3936,7 +3895,7 @@ def where(self, cond, other=None):
39363895
# it's float) if there are NaN values in our output.
39373896
dtype = None
39383897

3939-
return self._shallow_copy_with_infer(values, dtype=dtype)
3898+
return Index(values, dtype=dtype, name=self.name)
39403899

39413900
# construction helpers
39423901
@classmethod
@@ -4166,7 +4125,8 @@ def _concat_same_dtype(self, to_concat, name):
41664125

41674126
to_concat = [x._values if isinstance(x, Index) else x for x in to_concat]
41684127

4169-
return self._shallow_copy_with_infer(np.concatenate(to_concat), **attribs)
4128+
res_values = np.concatenate(to_concat)
4129+
return Index(res_values, name=name)
41704130

41714131
def putmask(self, mask, value):
41724132
"""
@@ -5208,7 +5168,7 @@ def insert(self, loc: int, item):
52085168
arr = np.asarray(self)
52095169
item = self._coerce_scalar_to_index(item)._values
52105170
idx = np.concatenate((arr[:loc], item, arr[loc:]))
5211-
return self._shallow_copy_with_infer(idx)
5171+
return Index(idx, name=self.name)
52125172

52135173
def drop(self, labels, errors: str_t = "raise"):
52145174
"""

pandas/core/indexes/category.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ def map(self, mapper):
690690
>>> idx.map({'a': 'first', 'b': 'second'})
691691
Index(['first', 'second', nan], dtype='object')
692692
"""
693-
return self._shallow_copy_with_infer(self._values.map(mapper))
693+
mapped = self._values.map(mapper)
694+
return Index(mapped, name=self.name)
694695

695696
def delete(self, loc):
696697
"""

pandas/core/indexes/datetimes.py

-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
214214
_attributes = ["name", "tz", "freq"]
215215

216216
_is_numeric_dtype = False
217-
_infer_as_myclass = True
218217

219218
_data: DatetimeArray
220219
tz: Optional[tzinfo]

pandas/core/indexes/multi.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1048,16 +1048,17 @@ def _shallow_copy(
10481048
result._cache.pop("levels", None) # GH32669
10491049
return result
10501050

1051-
def _shallow_copy_with_infer(self, values, **kwargs):
1052-
# On equal MultiIndexes the difference is empty.
1051+
def symmetric_difference(self, other, result_name=None, sort=None):
1052+
# On equal symmetric_difference MultiIndexes the difference is empty.
10531053
# Therefore, an empty MultiIndex is returned GH13490
1054-
if len(values) == 0:
1054+
tups = Index.symmetric_difference(self, other, result_name, sort)
1055+
if len(tups) == 0:
10551056
return MultiIndex(
10561057
levels=[[] for _ in range(self.nlevels)],
10571058
codes=[[] for _ in range(self.nlevels)],
1058-
**kwargs,
1059+
names=tups.name,
10591060
)
1060-
return self._shallow_copy(values, **kwargs)
1061+
return type(self).from_tuples(tups, names=tups.name)
10611062

10621063
# --------------------------------------------------------------------
10631064

pandas/core/indexes/period.py

-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index):
147147

148148
# define my properties & methods for delegation
149149
_is_numeric_dtype = False
150-
_infer_as_myclass = True
151150

152151
_data: PeriodArray
153152
freq: DateOffset

pandas/core/indexes/timedeltas.py

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ class TimedeltaIndex(DatetimeTimedeltaMixin):
111111
_comparables = ["name", "freq"]
112112
_attributes = ["name", "freq"]
113113
_is_numeric_dtype = True
114-
_infer_as_myclass = True
115114

116115
_data: TimedeltaArray
117116

pandas/core/tools/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def to_numeric(arg, errors="raise", downcast=None):
188188
return pd.Series(values, index=arg.index, name=arg.name)
189189
elif is_index:
190190
# because we want to coerce to numeric if possible,
191-
# do not use _shallow_copy_with_infer
191+
# do not use _shallow_copy
192192
return pd.Index(values, name=arg.name)
193193
elif is_scalars:
194194
return values[0]

0 commit comments

Comments
 (0)