Skip to content

Commit 70820ec

Browse files
jbrockmendelquintusdias
authored andcommitted
CLN: remove _maybe_update_attributes (pandas-dev#27896)
1 parent 0afa158 commit 70820ec

File tree

6 files changed

+26
-30
lines changed

6 files changed

+26
-30
lines changed

pandas/core/arrays/timedeltas.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -776,12 +776,14 @@ def __rdivmod__(self, other):
776776
res2 = other - res1 * self
777777
return res1, res2
778778

779-
# Note: TimedeltaIndex overrides this in call to cls._add_numeric_methods
780779
def __neg__(self):
781780
if self.freq is not None:
782781
return type(self)(-self._data, freq=-self.freq)
783782
return type(self)(-self._data)
784783

784+
def __pos__(self):
785+
return type(self)(self._data, freq=self.freq)
786+
785787
def __abs__(self):
786788
# Note: freq is not preserved
787789
return type(self)(np.abs(self._data))

pandas/core/indexes/base.py

-8
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,6 @@ def __array_wrap__(self, result, context=None):
695695
return result
696696

697697
attrs = self._get_attributes_dict()
698-
attrs = self._maybe_update_attributes(attrs)
699698
return Index(result, **attrs)
700699

701700
@cache_readonly
@@ -5335,12 +5334,6 @@ def _add_numeric_methods_disabled(cls):
53355334
cls.__abs__ = make_invalid_op("__abs__")
53365335
cls.__inv__ = make_invalid_op("__inv__")
53375336

5338-
def _maybe_update_attributes(self, attrs):
5339-
"""
5340-
Update Index attributes (e.g. freq) depending on op.
5341-
"""
5342-
return attrs
5343-
53445337
@classmethod
53455338
def _add_numeric_methods_binary(cls):
53465339
"""
@@ -5374,7 +5367,6 @@ def _make_evaluate_unary(op, opstr):
53745367
def _evaluate_numeric_unary(self):
53755368

53765369
attrs = self._get_attributes_dict()
5377-
attrs = self._maybe_update_attributes(attrs)
53785370
return Index(op(self.values), **attrs)
53795371

53805372
_evaluate_numeric_unary.__name__ = opstr

pandas/core/indexes/datetimelike.py

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from pandas.core.dtypes.common import (
1717
ensure_int64,
18+
is_bool_dtype,
1819
is_dtype_equal,
1920
is_float,
2021
is_integer,
@@ -163,6 +164,20 @@ def values(self):
163164
def asi8(self):
164165
return self._data.asi8
165166

167+
def __array_wrap__(self, result, context=None):
168+
"""
169+
Gets called after a ufunc.
170+
"""
171+
result = lib.item_from_zerodim(result)
172+
if is_bool_dtype(result) or lib.is_scalar(result):
173+
return result
174+
175+
attrs = self._get_attributes_dict()
176+
if not is_period_dtype(self) and attrs["freq"]:
177+
# no need to infer if freq is None
178+
attrs["freq"] = "infer"
179+
return Index(result, **attrs)
180+
166181
# ------------------------------------------------------------------------
167182

168183
def equals(self, other):

pandas/core/indexes/datetimes.py

-8
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,6 @@ def _convert_for_op(self, value):
465465
return _to_M8(value)
466466
raise ValueError("Passed item and index have different timezone")
467467

468-
def _maybe_update_attributes(self, attrs):
469-
""" Update Index attributes (e.g. freq) depending on op """
470-
freq = attrs.get("freq", None)
471-
if freq is not None:
472-
# no need to infer if freq is None
473-
attrs["freq"] = "infer"
474-
return attrs
475-
476468
# --------------------------------------------------------------------
477469
# Rendering Methods
478470

pandas/core/indexes/range.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class RangeIndex(Int64Index):
7575
_engine_type = libindex.Int64Engine
7676
_range = None # type: range
7777

78-
# check whether self._data has benn called
78+
# check whether self._data has been called
7979
_cached_data = None # type: np.ndarray
8080
# --------------------------------------------------------------------
8181
# Constructors
@@ -785,7 +785,6 @@ def _evaluate_numeric_binop(self, other):
785785

786786
other = extract_array(other, extract_numpy=True)
787787
attrs = self._get_attributes_dict()
788-
attrs = self._maybe_update_attributes(attrs)
789788

790789
left, right = self, other
791790

pandas/core/indexes/timedeltas.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ class TimedeltaDelegateMixin(DatetimelikeDelegateMixin):
4444
# which we we dont' want to expose in the .dt accessor.
4545
_delegate_class = TimedeltaArray
4646
_delegated_properties = TimedeltaArray._datetimelike_ops + ["components"]
47-
_delegated_methods = TimedeltaArray._datetimelike_methods + ["_box_values"]
47+
_delegated_methods = TimedeltaArray._datetimelike_methods + [
48+
"_box_values",
49+
"__neg__",
50+
"__pos__",
51+
"__abs__",
52+
]
4853
_raw_properties = {"components"}
4954
_raw_methods = {"to_pytimedelta"}
5055

@@ -56,7 +61,7 @@ class TimedeltaDelegateMixin(DatetimelikeDelegateMixin):
5661
TimedeltaArray,
5762
TimedeltaDelegateMixin._delegated_methods,
5863
typ="method",
59-
overwrite=False,
64+
overwrite=True,
6065
)
6166
class TimedeltaIndex(
6267
DatetimeIndexOpsMixin, dtl.TimelikeOps, Int64Index, TimedeltaDelegateMixin
@@ -279,14 +284,6 @@ def __setstate__(self, state):
279284

280285
_unpickle_compat = __setstate__
281286

282-
def _maybe_update_attributes(self, attrs):
283-
""" Update Index attributes (e.g. freq) depending on op """
284-
freq = attrs.get("freq", None)
285-
if freq is not None:
286-
# no need to infer if freq is None
287-
attrs["freq"] = "infer"
288-
return attrs
289-
290287
# -------------------------------------------------------------------
291288
# Rendering Methods
292289

@@ -689,7 +686,6 @@ def delete(self, loc):
689686

690687

691688
TimedeltaIndex._add_comparison_ops()
692-
TimedeltaIndex._add_numeric_methods_unary()
693689
TimedeltaIndex._add_logical_methods_disabled()
694690
TimedeltaIndex._add_datetimelike_methods()
695691

0 commit comments

Comments
 (0)