Skip to content

CLN: remove shallow_copy_with_infer #33691

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
Apr 22, 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
52 changes: 6 additions & 46 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,6 @@ def _outer_indexer(self, left, right):
# would we like our indexing holder to defer to us
_defer_to_indexing = False

# prioritize current class for _shallow_copy_with_infer,
# used to infer integers as datetime-likes
_infer_as_myclass = False

_engine_type = libindex.ObjectEngine
# whether we support partial string indexing. Overridden
# in DatetimeIndex and PeriodIndex
Expand Down Expand Up @@ -441,10 +437,6 @@ def __new__(
_simple_new), but fills caller's metadata otherwise specified. Passed
kwargs will overwrite corresponding metadata.

- _shallow_copy_with_infer: It returns new Index inferring its type
from passed values. It fills caller's metadata otherwise specified as the
same as _shallow_copy.

See each method's docstring.
"""

Expand Down Expand Up @@ -517,35 +509,6 @@ def _shallow_copy(self, values=None, name: Label = no_default):
result._cache = cache
return result

def _shallow_copy_with_infer(self, values, **kwargs):
"""
Create a new Index inferring the class with passed value, don't copy
the data, use the same object attributes with passed in attributes
taking precedence.

*this is an internal non-public method*

Parameters
----------
values : the values to create the new Index, optional
kwargs : updates the default attributes for this Index
"""
attributes = self._get_attributes_dict()
attributes.update(kwargs)
attributes["copy"] = False
if not len(values) and "dtype" not in kwargs:
# TODO: what if hasattr(values, "dtype")?
attributes["dtype"] = self.dtype
if self._infer_as_myclass:
try:
return self._constructor(values, **attributes)
except (TypeError, ValueError):
pass

# Remove tz so Index will try non-DatetimeIndex inference
attributes.pop("tz", None)
return Index(values, **attributes)

def is_(self, other) -> bool:
"""
More flexible, faster check like ``is`` but that works through views.
Expand Down Expand Up @@ -2810,11 +2773,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
except TypeError:
pass

attribs = self._get_attributes_dict()
attribs["name"] = result_name
if "freq" in attribs:
attribs["freq"] = None
return self._shallow_copy_with_infer(the_diff, **attribs)
return Index(the_diff, dtype=self.dtype, name=result_name)

def _assert_can_do_setop(self, other):
if not is_list_like(other):
Expand Down Expand Up @@ -3388,7 +3347,7 @@ def _reindex_non_unique(self, target):
new_indexer = np.arange(len(self.take(indexer)))
new_indexer[~check] = -1

new_index = self._shallow_copy_with_infer(new_labels)
new_index = Index(new_labels, name=self.name)
return new_index, indexer, new_indexer

# --------------------------------------------------------------------
Expand Down Expand Up @@ -3945,7 +3904,7 @@ def where(self, cond, other=None):
# it's float) if there are NaN values in our output.
dtype = None

return self._shallow_copy_with_infer(values, dtype=dtype)
return Index(values, dtype=dtype, name=self.name)

# construction helpers
@classmethod
Expand Down Expand Up @@ -4175,7 +4134,8 @@ def _concat_same_dtype(self, to_concat, name):

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

return self._shallow_copy_with_infer(np.concatenate(to_concat), **attribs)
res_values = np.concatenate(to_concat)
return Index(res_values, name=name)

def putmask(self, mask, value):
"""
Expand Down Expand Up @@ -5217,7 +5177,7 @@ def insert(self, loc: int, item):
arr = np.asarray(self)
item = self._coerce_scalar_to_index(item)._values
idx = np.concatenate((arr[:loc], item, arr[loc:]))
return self._shallow_copy_with_infer(idx)
return Index(idx, name=self.name)

def drop(self, labels, errors: str_t = "raise"):
"""
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ def map(self, mapper):
>>> idx.map({'a': 'first', 'b': 'second'})
Index(['first', 'second', nan], dtype='object')
"""
return self._shallow_copy_with_infer(self._values.map(mapper))
mapped = self._values.map(mapper)
return Index(mapped, name=self.name)

def delete(self, loc):
"""
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
_attributes = ["name", "tz", "freq"]

_is_numeric_dtype = False
_infer_as_myclass = True

_data: DatetimeArray
tz: Optional[tzinfo]
Expand Down
11 changes: 6 additions & 5 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,16 +1046,17 @@ def _shallow_copy(
result._cache.pop("levels", None) # GH32669
return result

def _shallow_copy_with_infer(self, values, **kwargs):
# On equal MultiIndexes the difference is empty.
def symmetric_difference(self, other, result_name=None, sort=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you really rename this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

symmetric_difference is the only method for which the logic in MultiIndex._shallow_copy_with_infer was needed, so basically yes.

# On equal symmetric_difference MultiIndexes the difference is empty.
# Therefore, an empty MultiIndex is returned GH13490
if len(values) == 0:
tups = Index.symmetric_difference(self, other, result_name, sort)
if len(tups) == 0:
return MultiIndex(
levels=[[] for _ in range(self.nlevels)],
codes=[[] for _ in range(self.nlevels)],
**kwargs,
names=tups.name,
)
return self._shallow_copy(values, **kwargs)
return type(self).from_tuples(tups, names=tups.name)

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

Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index):

# define my properties & methods for delegation
_is_numeric_dtype = False
_infer_as_myclass = True

_data: PeriodArray
freq: DateOffset
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ class TimedeltaIndex(DatetimeTimedeltaMixin, dtl.TimelikeOps):
_comparables = ["name", "freq"]
_attributes = ["name", "freq"]
_is_numeric_dtype = True
_infer_as_myclass = True

_data: TimedeltaArray

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def to_numeric(arg, errors="raise", downcast=None):
return pd.Series(values, index=arg.index, name=arg.name)
elif is_index:
# because we want to coerce to numeric if possible,
# do not use _shallow_copy_with_infer
# do not use _shallow_copy
return pd.Index(values, name=arg.name)
elif is_scalars:
return values[0]
Expand Down