Skip to content

REF: share __array_wrap__ #41643

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
May 25, 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
1 change: 1 addition & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ def __array_wrap__(self, result, context=None):
return result

attrs = self._get_attributes_dict()
attrs.pop("freq", None) # For DatetimeIndex/TimedeltaIndex
return Index(result, **attrs)

@cache_readonly
Expand Down
14 changes: 4 additions & 10 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
)

from pandas.core.dtypes.common import (
is_bool_dtype,
is_categorical_dtype,
is_dtype_equal,
is_integer,
Expand Down Expand Up @@ -113,15 +112,10 @@ def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc and other functions.
"""
result = lib.item_from_zerodim(result)
if is_bool_dtype(result) or lib.is_scalar(result):
return result

attrs = self._get_attributes_dict()
if not is_period_dtype(self.dtype) and attrs["freq"]:
# no need to infer if freq is None
attrs["freq"] = "infer"
return type(self)(result, **attrs)
out = super().__array_wrap__(result, context=context)
if isinstance(out, DatetimeTimedeltaMixin) and self.freq is not None:
out = out._with_freq("infer")
return out

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
_engine_type = libindex.DatetimeEngine
_supports_partial_string_indexing = True

_comparables = ["name", "freqstr", "tz"]
_attributes = ["name", "tz", "freq"]
_comparables = ["name", "freqstr"]
_attributes = ["name", "freq"]

_is_numeric_dtype = False

Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def func(self, other, sort=None):
class IntervalIndex(ExtensionIndex):
_typ = "intervalindex"
_comparables = ["name"]
_attributes = ["name", "closed"]
_attributes = ["name"]

# annotate properties pinned via inherit_names
closed: str
Expand Down Expand Up @@ -422,12 +422,8 @@ def __contains__(self, key: Any) -> bool:
def _multiindex(self) -> MultiIndex:
return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"])

def __array_wrap__(self, result, context=None):
# we don't want the superclass implementation
return result

def __reduce__(self):
d = {"left": self.left, "right": self.right}
d = {"left": self.left, "right": self.right, "closed": self.closed}
d.update(self._get_attributes_dict())
return _new_IntervalIndex, (type(self), d), None

Expand Down
37 changes: 0 additions & 37 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from pandas.util._decorators import doc

from pandas.core.dtypes.common import (
is_bool_dtype,
is_datetime64_any_dtype,
is_float,
is_integer,
Expand Down Expand Up @@ -350,42 +349,6 @@ def __contains__(self, key: Any) -> bool:
# ------------------------------------------------------------------------
# Index Methods

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc and other functions.

Needs additional handling as PeriodIndex stores internal data as int
dtype

Replace this to __numpy_ufunc__ in future version and implement
__array_function__ for Indexes
"""
if isinstance(context, tuple) and len(context) > 0:
func = context[0]
if func is np.add:
pass
elif func is np.subtract:
name = self.name
left = context[1][0]
right = context[1][1]
if isinstance(left, PeriodIndex) and isinstance(right, PeriodIndex):
name = left.name if left.name == right.name else None
return Index(result, name=name)
elif isinstance(left, Period) or isinstance(right, Period):
return Index(result, name=name)
elif isinstance(func, np.ufunc):
if "M->M" not in func.types:
msg = f"ufunc '{func.__name__}' not supported for the PeriodIndex"
# This should be TypeError, but TypeError cannot be raised
# from here because numpy catches.
raise ValueError(msg)

if is_bool_dtype(result):
return result
# the result is object dtype array of Period
# cannot pass _simple_new as it is
return type(self)(result, freq=self.freq, name=self.name)

def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray:
"""
where : array of timestamps
Expand Down