Skip to content

Commit 35ee710

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: share __array_wrap__ (pandas-dev#41643)
1 parent 00bd7d3 commit 35ee710

File tree

5 files changed

+9
-55
lines changed

5 files changed

+9
-55
lines changed

pandas/core/indexes/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ def __array_wrap__(self, result, context=None):
813813
return result
814814

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

818819
@cache_readonly

pandas/core/indexes/datetimelike.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
)
3636

3737
from pandas.core.dtypes.common import (
38-
is_bool_dtype,
3938
is_categorical_dtype,
4039
is_dtype_equal,
4140
is_integer,
@@ -113,15 +112,10 @@ def __array_wrap__(self, result, context=None):
113112
"""
114113
Gets called after a ufunc and other functions.
115114
"""
116-
result = lib.item_from_zerodim(result)
117-
if is_bool_dtype(result) or lib.is_scalar(result):
118-
return result
119-
120-
attrs = self._get_attributes_dict()
121-
if not is_period_dtype(self.dtype) and attrs["freq"]:
122-
# no need to infer if freq is None
123-
attrs["freq"] = "infer"
124-
return type(self)(result, **attrs)
115+
out = super().__array_wrap__(result, context=context)
116+
if isinstance(out, DatetimeTimedeltaMixin) and self.freq is not None:
117+
out = out._with_freq("infer")
118+
return out
125119

126120
# ------------------------------------------------------------------------
127121

pandas/core/indexes/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
257257
_engine_type = libindex.DatetimeEngine
258258
_supports_partial_string_indexing = True
259259

260-
_comparables = ["name", "freqstr", "tz"]
261-
_attributes = ["name", "tz", "freq"]
260+
_comparables = ["name", "freqstr"]
261+
_attributes = ["name", "freq"]
262262

263263
_is_numeric_dtype = False
264264

pandas/core/indexes/interval.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def func(self, other, sort=None):
253253
class IntervalIndex(ExtensionIndex):
254254
_typ = "intervalindex"
255255
_comparables = ["name"]
256-
_attributes = ["name", "closed"]
256+
_attributes = ["name"]
257257

258258
# annotate properties pinned via inherit_names
259259
closed: str
@@ -419,12 +419,8 @@ def __contains__(self, key: Any) -> bool:
419419
def _multiindex(self) -> MultiIndex:
420420
return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"])
421421

422-
def __array_wrap__(self, result, context=None):
423-
# we don't want the superclass implementation
424-
return result
425-
426422
def __reduce__(self):
427-
d = {"left": self.left, "right": self.right}
423+
d = {"left": self.left, "right": self.right, "closed": self.closed}
428424
d.update(self._get_attributes_dict())
429425
return _new_IntervalIndex, (type(self), d), None
430426

pandas/core/indexes/period.py

-37
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from pandas.util._decorators import doc
3535

3636
from pandas.core.dtypes.common import (
37-
is_bool_dtype,
3837
is_datetime64_any_dtype,
3938
is_float,
4039
is_integer,
@@ -350,42 +349,6 @@ def __contains__(self, key: Any) -> bool:
350349
# ------------------------------------------------------------------------
351350
# Index Methods
352351

353-
def __array_wrap__(self, result, context=None):
354-
"""
355-
Gets called after a ufunc and other functions.
356-
357-
Needs additional handling as PeriodIndex stores internal data as int
358-
dtype
359-
360-
Replace this to __numpy_ufunc__ in future version and implement
361-
__array_function__ for Indexes
362-
"""
363-
if isinstance(context, tuple) and len(context) > 0:
364-
func = context[0]
365-
if func is np.add:
366-
pass
367-
elif func is np.subtract:
368-
name = self.name
369-
left = context[1][0]
370-
right = context[1][1]
371-
if isinstance(left, PeriodIndex) and isinstance(right, PeriodIndex):
372-
name = left.name if left.name == right.name else None
373-
return Index(result, name=name)
374-
elif isinstance(left, Period) or isinstance(right, Period):
375-
return Index(result, name=name)
376-
elif isinstance(func, np.ufunc):
377-
if "M->M" not in func.types:
378-
msg = f"ufunc '{func.__name__}' not supported for the PeriodIndex"
379-
# This should be TypeError, but TypeError cannot be raised
380-
# from here because numpy catches.
381-
raise ValueError(msg)
382-
383-
if is_bool_dtype(result):
384-
return result
385-
# the result is object dtype array of Period
386-
# cannot pass _simple_new as it is
387-
return type(self)(result, freq=self.freq, name=self.name)
388-
389352
def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray:
390353
"""
391354
where : array of timestamps

0 commit comments

Comments
 (0)