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 1 commit
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")
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,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 @@ -2662,7 +2662,7 @@ def _getitem_multilevel(self, key):
result = self._constructor(
new_values, index=self.index, columns=result_columns
)
result = result.__finalize__(self)
result = result.__finalize__(self, method="_getitem_multilevel")

# If there is only one column being returned, and its name is
# either an empty string, or a tuple with an empty string as its
Expand Down Expand Up @@ -4480,7 +4480,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 @@ -4808,7 +4808,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 @@ -4944,7 +4944,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
90 changes: 55 additions & 35 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def _single_replace(self, to_replace, method, inplace, limit):
if values.dtype == orig_dtype and inplace:
return

result = pd.Series(values, index=self.index, dtype=self.dtype).__finalize__(self)
result = pd.Series(values, index=self.index, dtype=self.dtype).__finalize__(
self, method="_single_replace"
)
Copy link
Member

Choose a reason for hiding this comment

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

can this be broken into two lines? probably also should use _constructor instead of pd.Series


if inplace:
self._update_inplace(result)
Expand Down Expand Up @@ -588,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 @@ -991,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 +1361,7 @@ def __invert__(self):
return self

new_data = self._data.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 +1806,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 @@ -3360,7 +3366,7 @@ class max_speed
new_data = self._data.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 @@ -3567,7 +3573,7 @@ def _slice(self: FrameOrSeries, slobj: slice, axis=0) -> FrameOrSeries:
assert isinstance(slobj, slice), type(slobj)
axis = self._get_block_manager_axis(axis)
result = self._constructor(self._data.get_slice(slobj, axis=axis))
result = result.__finalize__(self)
result = result.__finalize__(self, method="_slice")

# this could be a view
# but only in a single-dtyped view sliceable case
Expand Down Expand Up @@ -4433,7 +4439,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 @@ -4506,7 +4512,9 @@ def _reindex_with_indexers(
if copy and new_data is self._data:
new_data = new_data.copy()

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

def filter(
self: FrameOrSeries,
Expand Down Expand Up @@ -5132,7 +5140,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 Down Expand Up @@ -5268,7 +5276,9 @@ def _consolidate(self, inplace: bool_t = False):
else:
f = lambda: self._data.consolidate()
cons_data = self._protect_consolidate(f)
return self._constructor(cons_data).__finalize__(self)
return self._constructor(cons_data).__finalize__(
self, method="_consolidate"
)

@property
def _is_mixed_type(self) -> bool_t:
Expand Down Expand Up @@ -5297,10 +5307,14 @@ def _check_inplace_setting(self, value) -> bool_t:
return True

def _get_numeric_data(self):
return self._constructor(self._data.get_numeric_data()).__finalize__(self)
return self._constructor(self._data.get_numeric_data()).__finalize__(
self, method="_get_numeric_data"
)

def _get_bool_data(self):
return self._constructor(self._data.get_bool_data()).__finalize__(self)
return self._constructor(self._data.get_bool_data()).__finalize__(
self, method="_get_bool_data"
)

# ----------------------------------------------------------------------
# Internal Interface Methods
Expand Down Expand Up @@ -5427,7 +5441,7 @@ def _to_dict_of_blocks(self, copy: bool_t = True):
Internal ONLY
"""
return {
k: self._constructor(v).__finalize__(self)
k: self._constructor(v).__finalize__(self, mehtod="_to_dict_of_blocks")
for k, v, in self._data.to_dict(copy=copy).items()
}

Expand Down Expand Up @@ -5567,7 +5581,7 @@ def astype(
else:
# else, only a single dtype is given
new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors)
return self._constructor(new_data).__finalize__(self)
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 @@ -5680,7 +5694,7 @@ def copy(self: FrameOrSeries, deep: bool_t = True) -> FrameOrSeries:
dtype: object
"""
data = self._data.copy(deep=deep)
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 @@ -5739,7 +5753,7 @@ def _convert(
coerce=coerce,
copy=copy,
)
).__finalize__(self)
).__finalize__(self, method="_convert")

def infer_objects(self: FrameOrSeries) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -5788,7 +5802,7 @@ def infer_objects(self: FrameOrSeries) -> FrameOrSeries:
self._data.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 @@ -6115,7 +6129,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 @@ -6631,7 +6645,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 @@ -6897,7 +6911,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 @@ -7135,11 +7149,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 @@ -7205,11 +7219,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 @@ -8223,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 @@ -8430,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, method="_align_frame"),
right.__finalize__(other, method="_align_frame"),
)

def _align_series(
self,
Expand Down Expand Up @@ -8514,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, method="_align_series"),
right.__finalize__(other, method="_align_series"),
)

def _where(
self,
Expand Down Expand Up @@ -8655,7 +8675,7 @@ def _where(
axis=block_axis,
)
result = self._constructor(new_data)
return result.__finalize__(self)
return result.__finalize__(self, method="_where")

_shared_docs[
"where"
Expand Down Expand Up @@ -8927,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 @@ -8964,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 @@ -9024,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 @@ -9235,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 @@ -9404,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 @@ -11183,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
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,7 +2342,7 @@ def _get_values_for_loc(self, series: "Series", loc, key):
new_index = self[loc]
new_index = maybe_droplevels(new_index, key)
new_ser = series._constructor(new_values, index=new_index, name=series.name)
return new_ser.__finalize__(series)
return new_ser.__finalize__(series, method="_get_values_for_loc")

def _convert_listlike_indexer(self, keyarr):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def _construct_result(
# We do not pass dtype to ensure that the Series constructor
# does inference in the case where `result` has object-dtype.
out = left._constructor(result, index=index)
out = out.__finalize__(left)
out = out.__finalize__(left, method="_construct_result")

# Set the result's name after __finalize__ is called because __finalize__
# would set it back to self.name
Expand Down
Loading