Skip to content

CLN: remove _maybe_update_attributes #27896

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 5 commits into from
Aug 15, 2019
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
4 changes: 3 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,12 +776,14 @@ def __rdivmod__(self, other):
res2 = other - res1 * self
return res1, res2

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

def __pos__(self):
return type(self)(self._data, freq=self.freq)
Copy link
Member

Choose a reason for hiding this comment

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

This is new? Or where was it done before?

Copy link
Contributor

Choose a reason for hiding this comment

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

In _add_numeric_methods_unary.


def __abs__(self):
# Note: freq is not preserved
return type(self)(np.abs(self._data))
Expand Down
8 changes: 0 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ def __array_wrap__(self, result, context=None):
return result

attrs = self._get_attributes_dict()
attrs = self._maybe_update_attributes(attrs)
return Index(result, **attrs)

@cache_readonly
Expand Down Expand Up @@ -5335,12 +5334,6 @@ def _add_numeric_methods_disabled(cls):
cls.__abs__ = make_invalid_op("__abs__")
cls.__inv__ = make_invalid_op("__inv__")

def _maybe_update_attributes(self, attrs):
"""
Update Index attributes (e.g. freq) depending on op.
"""
return attrs

@classmethod
def _add_numeric_methods_binary(cls):
"""
Expand Down Expand Up @@ -5374,7 +5367,6 @@ def _make_evaluate_unary(op, opstr):
def _evaluate_numeric_unary(self):

attrs = self._get_attributes_dict()
attrs = self._maybe_update_attributes(attrs)
return Index(op(self.values), **attrs)

_evaluate_numeric_unary.__name__ = opstr
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from pandas.core.dtypes.common import (
ensure_int64,
is_bool_dtype,
is_dtype_equal,
is_float,
is_integer,
Expand Down Expand Up @@ -163,6 +164,20 @@ def values(self):
def asi8(self):
return self._data.asi8

def __array_wrap__(self, result, context=None):
Copy link
Member

Choose a reason for hiding this comment

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

sidenote (not for this PR): we should move to __array_ufunc__ here instead

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed on this, can you create an issue @jbrockmendel

"""
Gets called after a ufunc.
"""
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) and attrs["freq"]:
# no need to infer if freq is None
attrs["freq"] = "infer"
return Index(result, **attrs)

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

def equals(self, other):
Expand Down
8 changes: 0 additions & 8 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,6 @@ def _convert_for_op(self, value):
return _to_M8(value)
raise ValueError("Passed item and index have different timezone")

def _maybe_update_attributes(self, attrs):
""" Update Index attributes (e.g. freq) depending on op """
freq = attrs.get("freq", None)
if freq is not None:
# no need to infer if freq is None
attrs["freq"] = "infer"
return attrs

# --------------------------------------------------------------------
# Rendering Methods

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class RangeIndex(Int64Index):
_engine_type = libindex.Int64Engine
_range = None # type: range

# check whether self._data has benn called
# check whether self._data has been called
_cached_data = None # type: np.ndarray
# --------------------------------------------------------------------
# Constructors
Expand Down Expand Up @@ -785,7 +785,6 @@ def _evaluate_numeric_binop(self, other):

other = extract_array(other, extract_numpy=True)
attrs = self._get_attributes_dict()
attrs = self._maybe_update_attributes(attrs)

left, right = self, other

Expand Down
18 changes: 7 additions & 11 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ class TimedeltaDelegateMixin(DatetimelikeDelegateMixin):
# which we we dont' want to expose in the .dt accessor.
_delegate_class = TimedeltaArray
_delegated_properties = TimedeltaArray._datetimelike_ops + ["components"]
_delegated_methods = TimedeltaArray._datetimelike_methods + ["_box_values"]
_delegated_methods = TimedeltaArray._datetimelike_methods + [
"_box_values",
"__neg__",
"__pos__",
"__abs__",
]
_raw_properties = {"components"}
_raw_methods = {"to_pytimedelta"}

Expand All @@ -56,7 +61,7 @@ class TimedeltaDelegateMixin(DatetimelikeDelegateMixin):
TimedeltaArray,
TimedeltaDelegateMixin._delegated_methods,
typ="method",
overwrite=False,
overwrite=True,
)
class TimedeltaIndex(
DatetimeIndexOpsMixin, dtl.TimelikeOps, Int64Index, TimedeltaDelegateMixin
Expand Down Expand Up @@ -279,14 +284,6 @@ def __setstate__(self, state):

_unpickle_compat = __setstate__

def _maybe_update_attributes(self, attrs):
""" Update Index attributes (e.g. freq) depending on op """
freq = attrs.get("freq", None)
if freq is not None:
# no need to infer if freq is None
attrs["freq"] = "infer"
return attrs

# -------------------------------------------------------------------
# Rendering Methods

Expand Down Expand Up @@ -689,7 +686,6 @@ def delete(self, loc):


TimedeltaIndex._add_comparison_ops()
TimedeltaIndex._add_numeric_methods_unary()
TimedeltaIndex._add_logical_methods_disabled()
TimedeltaIndex._add_datetimelike_methods()

Expand Down