Skip to content

CLN: unify __finalize__ treatment for Series ops #28590

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 3 commits into from
Sep 26, 2019
Merged
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
27 changes: 2 additions & 25 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,6 @@ def wrapper(self, other):

res_name = get_op_result_name(self, other)

# TODO: shouldn't we be applying finalize whenever
# not isinstance(other, ABCSeries)?
finalizer = (
lambda x: x.__finalize__(self)
if isinstance(other, (np.ndarray, ABCIndexClass))
else x
)
Copy link
Member

Choose a reason for hiding this comment

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

Now it will always be calling finalize. So was the comment there wrong? What if other is a Series? Does this have an impact on the result?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does this have an impact on the result?

In the overwhemingly-most-common case where there is no user-defined _metadata, no, this doesn't affect the result. Otherwise, it affects the result when other is a Series.


if isinstance(other, ABCDataFrame): # pragma: no cover
# Defer to DataFrame implementation; fail early
return NotImplemented
Expand All @@ -680,13 +672,7 @@ def wrapper(self, other):

res_values = comparison_op(lvalues, rvalues, op)

result = self._constructor(res_values, index=self.index)
result = finalizer(result)

# Set the result's name after finalizer is called because finalizer
# would set it back to self.name
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this comment inside the construct_result function?

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea, will do

result.name = res_name
return result
return _construct_result(self, res_values, index=self.index, name=res_name)

wrapper.__name__ = op_name
return wrapper
Expand All @@ -703,14 +689,6 @@ def wrapper(self, other):
self, other = _align_method_SERIES(self, other, align_asobject=True)
res_name = get_op_result_name(self, other)

# TODO: shouldn't we be applying finalize whenever
# not isinstance(other, ABCSeries)?
finalizer = (
lambda x: x.__finalize__(self)
if not isinstance(other, (ABCSeries, ABCIndexClass))
else x
)

if isinstance(other, ABCDataFrame):
# Defer to DataFrame implementation; fail early
return NotImplemented
Expand All @@ -719,8 +697,7 @@ def wrapper(self, other):
rvalues = extract_array(other, extract_numpy=True)

res_values = logical_op(lvalues, rvalues, op)
result = self._constructor(res_values, index=self.index, name=res_name)
return finalizer(result)
return _construct_result(self, res_values, index=self.index, name=res_name)

wrapper.__name__ = op_name
return wrapper
Expand Down