Skip to content

CLN: streamline Series _construct_result calls #28637

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 4 commits into from
Oct 1, 2019
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
32 changes: 14 additions & 18 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,22 @@ def _align_method_SERIES(left, right, align_asobject=False):
return left, right


def _construct_result(left, result, index, name, dtype=None):
def _construct_result(left, result, index, name):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a doc-string / types

Copy link
Member Author

Choose a reason for hiding this comment

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

request addressed + green

"""
If the raw op result has a non-None name (e.g. it is an Index object) and
the name argument is None, then passing name to the constructor will
not be enough; we still need to override the name attribute.
"""
out = left._constructor(result, index=index, dtype=dtype)
if isinstance(result, tuple):
# produced by divmod or rdivmod
return (
_construct_result(left, result[0], index=index, name=name),
_construct_result(left, result[1], index=index, name=name),
)

# 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)

# Set the result's name after __finalize__ is called because __finalize__
Expand All @@ -581,15 +590,6 @@ def _construct_result(left, result, index, name, dtype=None):
return out


def _construct_divmod_result(left, result, index, name, dtype=None):
"""divmod returns a tuple of like indexed series instead of a single series.
"""
return (
_construct_result(left, result[0], index=index, name=name, dtype=dtype),
_construct_result(left, result[1], index=index, name=name, dtype=dtype),
)


def _arith_method_SERIES(cls, op, special):
"""
Wrapper function for Series arithmetic operations, to avoid
Expand All @@ -598,9 +598,6 @@ def _arith_method_SERIES(cls, op, special):
str_rep = _get_opstr(op)
op_name = _get_op_name(op, special)
eval_kwargs = _gen_eval_kwargs(op_name)
construct_result = (
_construct_divmod_result if op in [divmod, rdivmod] else _construct_result
)

def wrapper(left, right):
if isinstance(right, ABCDataFrame):
Expand All @@ -612,9 +609,7 @@ def wrapper(left, right):
lvalues = extract_array(left, extract_numpy=True)
result = arithmetic_op(lvalues, right, op, str_rep, eval_kwargs)

# We do not pass dtype to ensure that the Series constructor
# does inference in the case where `result` has object-dtype.
return construct_result(left, result, index=left.index, name=res_name)
return _construct_result(left, result, index=left.index, name=res_name)

wrapper.__name__ = op_name
return wrapper
Expand Down Expand Up @@ -683,6 +678,7 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
# validate axis
if axis is not None:
self._get_axis_number(axis)

if isinstance(other, ABCSeries):
return self._binop(other, op, level=level, fill_value=fill_value)
elif isinstance(other, (np.ndarray, list, tuple)):
Expand All @@ -694,7 +690,7 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
if fill_value is not None:
self = self.fillna(fill_value)

return self._constructor(op(self, other), self.index).__finalize__(self)
return op(self, other)

flex_wrapper.__name__ = name
return flex_wrapper
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2738,10 +2738,7 @@ def _binop(self, other, func, level=None, fill_value=None):
result = func(this_vals, other_vals)

name = ops.get_op_result_name(self, other)
if func.__name__ in ["divmod", "rdivmod"]:
ret = ops._construct_divmod_result(self, result, new_index, name)
else:
ret = ops._construct_result(self, result, new_index, name)
ret = ops._construct_result(self, result, new_index, name)
return ret

def combine(self, other, func, fill_value=None):
Expand Down