-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: implement fill_value for df.add(other=Series) #13488 #32335
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,12 @@ | |
from pandas._typing import ArrayLike, Level | ||
from pandas.util._decorators import Appender | ||
|
||
from pandas.core.dtypes.common import is_list_like, is_timedelta64_dtype | ||
from pandas.core.dtypes.common import ( | ||
is_bool, | ||
is_list_like, | ||
is_number, | ||
is_timedelta64_dtype, | ||
) | ||
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries | ||
from pandas.core.dtypes.missing import isna | ||
|
||
|
@@ -341,7 +346,11 @@ def fill_binop(left, right, fill_value): | |
left = left.copy() | ||
left[left_mask & mask] = fill_value | ||
|
||
if right_mask.any(): | ||
if is_bool(right_mask): | ||
if right_mask: | ||
right = left._constructor(right, index=left.index) | ||
right[right_mask & mask] = fill_value | ||
elif right_mask.any(): | ||
# Avoid making a copy if we can | ||
right = right.copy() | ||
right[right_mask & mask] = fill_value | ||
|
@@ -585,7 +594,7 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0): | |
# DataFrame | ||
|
||
|
||
def _combine_series_frame(left, right, func, axis: int): | ||
def _combine_series_frame(left, right, func, axis: int, fill_value=None): | ||
""" | ||
Apply binary operator `func` to self, other using alignment and fill | ||
conventions determined by the axis argument. | ||
|
@@ -596,16 +605,29 @@ def _combine_series_frame(left, right, func, axis: int): | |
right : Series | ||
func : binary operator | ||
axis : {0, 1} | ||
fill_value : numeric, optional | ||
|
||
Returns | ||
------- | ||
result : DataFrame | ||
""" | ||
if fill_value is None: | ||
_arith_op = func | ||
|
||
else: | ||
|
||
def _arith_op(left, right): | ||
left, right = fill_binop(left, right, fill_value) | ||
return func(left, right) | ||
|
||
# We assume that self.align(other, ...) has already been called | ||
if axis == 0: | ||
new_data = left._combine_match_index(right, func) | ||
if fill_value is not None: | ||
new_data = dispatch_to_series(left, right, _arith_op, axis=0) | ||
else: | ||
new_data = left._combine_match_index(right, _arith_op) | ||
else: | ||
new_data = dispatch_to_series(left, right, func, axis="columns") | ||
new_data = dispatch_to_series(left, right, _arith_op, axis="columns") | ||
|
||
return left._construct_result(new_data) | ||
|
||
|
@@ -771,6 +793,12 @@ def f(self, other, axis=default_axis, level=None, fill_value=None): | |
if _should_reindex_frame_op(self, other, axis, default_axis, fill_value, level): | ||
return _frame_arith_method_with_reindex(self, other, op) | ||
|
||
if not is_number(fill_value) and fill_value is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think this condition is right. what if the series we are working with is e.g. datetime64? |
||
raise TypeError( | ||
"fill_value must be numeric or None. " | ||
f"Got {type(fill_value).__name__}" | ||
) | ||
|
||
self, other = _align_method_FRAME(self, other, axis, flex=True, level=level) | ||
|
||
if isinstance(other, ABCDataFrame): | ||
|
@@ -787,11 +815,8 @@ def f(self, other, axis=default_axis, level=None, fill_value=None): | |
pass_op = op if axis in [0, "columns", None] else na_op | ||
pass_op = pass_op if not is_logical else op | ||
|
||
if fill_value is not None: | ||
raise NotImplementedError(f"fill_value {fill_value} not supported.") | ||
|
||
axis = self._get_axis_number(axis) if axis is not None else 1 | ||
return _combine_series_frame(self, other, pass_op, axis=axis) | ||
return _combine_series_frame(self, other, pass_op, axis, fill_value) | ||
else: | ||
# in this case we always have `np.ndim(other) == 0` | ||
if fill_value is not None: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would go in the 1.1.0 file