Skip to content

Pass method in __finalize__ #33273

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 6 commits into from
Apr 6, 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
2 changes: 1 addition & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,4 +1521,4 @@ def duplicated(self, keep="first"):
else:
return self._constructor(
duplicated(self, keep=keep), index=self.index
).__finalize__(self)
).__finalize__(self, method="duplicated")
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2515,7 +2515,7 @@ def transpose(self, *args, copy: bool = False) -> "DataFrame":
new_values, index=self.columns, columns=self.index
)

return result.__finalize__(self)
return result.__finalize__(self, method="transpose")

@property
def T(self) -> "DataFrame":
Expand Down Expand Up @@ -4470,7 +4470,7 @@ def _maybe_casted_values(index, labels=None):
@Appender(_shared_docs["isna"] % _shared_doc_kwargs)
def isna(self) -> "DataFrame":
result = self._constructor(self._data.isna(func=isna))
return result.__finalize__(self)
return result.__finalize__(self, method="isna")

@Appender(_shared_docs["isna"] % _shared_doc_kwargs)
def isnull(self) -> "DataFrame":
Expand Down Expand Up @@ -4798,7 +4798,7 @@ def sort_values(
if inplace:
return self._update_inplace(result)
else:
return result.__finalize__(self)
return result.__finalize__(self, method="sort_values")

def sort_index(
self,
Expand Down Expand Up @@ -4934,7 +4934,7 @@ def sort_index(
if inplace:
return self._update_inplace(result)
else:
return result.__finalize__(self)
return result.__finalize__(self, method="sort_index")

def value_counts(
self,
Expand Down
77 changes: 46 additions & 31 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries:
if copy:
new_values = new_values.copy()

return self._constructor(new_values, *new_axes).__finalize__(self)
return self._constructor(new_values, *new_axes).__finalize__(
self, method="swapaxes"
)

def droplevel(self: FrameOrSeries, level, axis=0) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -993,7 +995,7 @@ def rename(
self._update_inplace(result)
return None
else:
return result.__finalize__(self)
return result.__finalize__(self, method="rename")

@rewrite_axis_style_signature("mapper", [("copy", True), ("inplace", False)])
def rename_axis(self, mapper=lib.no_default, **kwargs):
Expand Down Expand Up @@ -1357,7 +1359,7 @@ def __invert__(self):
return self

new_data = self._mgr.apply(operator.invert)
result = self._constructor(new_data).__finalize__(self)
result = self._constructor(new_data).__finalize__(self, method="__invert__")
return result

def __nonzero__(self):
Expand Down Expand Up @@ -1802,7 +1804,9 @@ def __array_wrap__(self, result, context=None):
# ptp also requires the item_from_zerodim
return result
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
return self._constructor(result, **d).__finalize__(self)
return self._constructor(result, **d).__finalize__(
self, method="__array_wrap__"
)

# ideally we would define this to avoid the getattr checks, but
# is slower
Expand Down Expand Up @@ -3361,7 +3365,7 @@ class max_speed
new_data = self._mgr.take(
indices, axis=self._get_block_manager_axis(axis), verify=True
)
return self._constructor(new_data).__finalize__(self)
return self._constructor(new_data).__finalize__(self, method="take")

def _take_with_is_copy(self: FrameOrSeries, indices, axis=0) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -4430,7 +4434,7 @@ def reindex(self: FrameOrSeries, *args, **kwargs) -> FrameOrSeries:
# perform the reindex on the axes
return self._reindex_axes(
axes, level, limit, tolerance, method, fill_value, copy
).__finalize__(self)
).__finalize__(self, method="reindex")

def _reindex_axes(
self: FrameOrSeries, axes, level, limit, tolerance, method, fill_value, copy
Expand Down Expand Up @@ -5129,7 +5133,7 @@ def pipe(self, func, *args, **kwargs):
# Attribute access

def __finalize__(
self: FrameOrSeries, other, method=None, **kwargs
self: FrameOrSeries, other, method: Optional[str] = None, **kwargs
) -> FrameOrSeries:
"""
Propagate metadata from other to self.
Expand All @@ -5138,9 +5142,14 @@ def __finalize__(
----------
other : the object from which to get the attributes that we are going
to propagate
method : optional, a passed method name ; possibly to take different
types of propagation actions based on this
method : str, optional
A passed method name providing context on where ``__finalize__``
was called.

.. warning:

The value passed as `method` are not currently considered
stable across pandas releases.
"""
if isinstance(other, NDFrame):
for name in other.attrs:
Expand Down Expand Up @@ -5293,10 +5302,10 @@ def _check_inplace_setting(self, value) -> bool_t:
return True

def _get_numeric_data(self):
return self._constructor(self._mgr.get_numeric_data()).__finalize__(self)
return self._constructor(self._mgr.get_numeric_data()).__finalize__(self,)

def _get_bool_data(self):
return self._constructor(self._mgr.get_bool_data()).__finalize__(self)
return self._constructor(self._mgr.get_bool_data()).__finalize__(self,)

# ----------------------------------------------------------------------
# Internal Interface Methods
Expand Down Expand Up @@ -5562,8 +5571,8 @@ def astype(

else:
# else, only a single dtype is given
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
return self._constructor(new_data).__finalize__(self)
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
return self._constructor(new_data).__finalize__(self, method="astype")

# GH 19920: retain column metadata after concat
result = pd.concat(results, axis=1, copy=False)
Expand Down Expand Up @@ -5677,7 +5686,7 @@ def copy(self: FrameOrSeries, deep: bool_t = True) -> FrameOrSeries:
"""
data = self._mgr.copy(deep=deep)
self._clear_item_cache()
return self._constructor(data).__finalize__(self)
return self._constructor(data).__finalize__(self, method="copy")

def __copy__(self: FrameOrSeries, deep: bool_t = True) -> FrameOrSeries:
return self.copy(deep=deep)
Expand Down Expand Up @@ -5783,7 +5792,7 @@ def infer_objects(self: FrameOrSeries) -> FrameOrSeries:
self._mgr.convert(
datetime=True, numeric=False, timedelta=True, coerce=False, copy=True
)
).__finalize__(self)
).__finalize__(self, method="infer_objects")

def convert_dtypes(
self: FrameOrSeries,
Expand Down Expand Up @@ -6110,7 +6119,7 @@ def fillna(
if inplace:
return self._update_inplace(result)
else:
return result.__finalize__(self)
return result.__finalize__(self, method="fillna")

def ffill(
self: FrameOrSeries,
Expand Down Expand Up @@ -6626,7 +6635,7 @@ def replace(
if inplace:
return self._update_inplace(result)
else:
return result.__finalize__(self)
return result.__finalize__(self, method="replace")

_shared_docs[
"interpolate"
Expand Down Expand Up @@ -6892,7 +6901,7 @@ def interpolate(
if inplace:
return self._update_inplace(result)
else:
return result.__finalize__(self)
return result.__finalize__(self, method="interpolate")

# ----------------------------------------------------------------------
# Timeseries methods Methods
Expand Down Expand Up @@ -7130,11 +7139,11 @@ def asof(self, where, subset=None):

@Appender(_shared_docs["isna"] % _shared_doc_kwargs)
def isna(self: FrameOrSeries) -> FrameOrSeries:
return isna(self).__finalize__(self)
return isna(self).__finalize__(self, method="isna")

@Appender(_shared_docs["isna"] % _shared_doc_kwargs)
def isnull(self: FrameOrSeries) -> FrameOrSeries:
return isna(self).__finalize__(self)
return isna(self).__finalize__(self, method="isnull")

_shared_docs[
"notna"
Expand Down Expand Up @@ -7200,11 +7209,11 @@ def isnull(self: FrameOrSeries) -> FrameOrSeries:

@Appender(_shared_docs["notna"] % _shared_doc_kwargs)
def notna(self: FrameOrSeries) -> FrameOrSeries:
return notna(self).__finalize__(self)
return notna(self).__finalize__(self, method="notna")

@Appender(_shared_docs["notna"] % _shared_doc_kwargs)
def notnull(self: FrameOrSeries) -> FrameOrSeries:
return notna(self).__finalize__(self)
return notna(self).__finalize__(self, method="notnull")

def _clip_with_scalar(self, lower, upper, inplace: bool_t = False):
if (lower is not None and np.any(isna(lower))) or (
Expand Down Expand Up @@ -8228,7 +8237,7 @@ def ranker(data):
pct=pct,
)
ranks = self._constructor(ranks, **data._construct_axes_dict())
return ranks.__finalize__(self)
return ranks.__finalize__(self, method="rank")

# if numeric_only is None, and we can't get anything, we try with
# numeric_only=True
Expand Down Expand Up @@ -8435,7 +8444,10 @@ def _align_frame(
left.index = join_index
right.index = join_index

return left.__finalize__(self), right.__finalize__(other)
return (
left.__finalize__(self),
right.__finalize__(other),
)

def _align_series(
self,
Expand Down Expand Up @@ -8519,7 +8531,10 @@ def _align_series(
left.index = join_index
right.index = join_index

return left.__finalize__(self), right.__finalize__(other)
return (
left.__finalize__(self),
right.__finalize__(other),
)

def _where(
self,
Expand Down Expand Up @@ -8932,7 +8947,7 @@ def shift(
else:
return self.tshift(periods, freq)

return self._constructor(new_data).__finalize__(self)
return self._constructor(new_data).__finalize__(self, method="shift")

def slice_shift(self: FrameOrSeries, periods: int = 1, axis=0) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -8969,7 +8984,7 @@ def slice_shift(self: FrameOrSeries, periods: int = 1, axis=0) -> FrameOrSeries:
shifted_axis = self._get_axis(axis)[islicer]
new_obj.set_axis(shifted_axis, axis=axis, inplace=True)

return new_obj.__finalize__(self)
return new_obj.__finalize__(self, method="slice_shift")

def tshift(
self: FrameOrSeries, periods: int = 1, freq=None, axis: Axis = 0
Expand Down Expand Up @@ -9029,7 +9044,7 @@ def tshift(

result = self.copy()
result.set_axis(new_ax, axis, inplace=True)
return result.__finalize__(self)
return result.__finalize__(self, method="tshift")

def truncate(
self: FrameOrSeries, before=None, after=None, axis=None, copy: bool_t = True
Expand Down Expand Up @@ -9240,7 +9255,7 @@ def _tz_convert(ax, tz):

result = self.copy(deep=copy)
result = result.set_axis(ax, axis=axis, inplace=False)
return result.__finalize__(self)
return result.__finalize__(self, method="tz_convert")

def tz_localize(
self: FrameOrSeries,
Expand Down Expand Up @@ -9409,7 +9424,7 @@ def _tz_localize(ax, tz, ambiguous, nonexistent):

result = self.copy(deep=copy)
result = result.set_axis(ax, axis=axis, inplace=False)
return result.__finalize__(self)
return result.__finalize__(self, method="tz_localize")

# ----------------------------------------------------------------------
# Numeric Methods
Expand Down Expand Up @@ -11188,7 +11203,7 @@ def block_accum_func(blk_values):

d = self._construct_axes_dict()
d["copy"] = False
return self._constructor(result, **d).__finalize__(self)
return self._constructor(result, **d).__finalize__(self, method=name)

return set_function_name(cum_func, name, cls)

Expand Down
Loading