Skip to content

REF: remove unnecessary SparseDataFrame arith methods #28414

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
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8e1ad04
implement _construct_result
jbrockmendel Aug 20, 2019
bdc0d72
Change dispatch_to_series return, restore alignment
jbrockmendel Aug 20, 2019
7a4b8c2
remove comment
jbrockmendel Aug 20, 2019
3505e6a
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 20, 2019
63c5f80
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 20, 2019
bf0d2bf
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 20, 2019
c235b6a
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 23, 2019
6cf996d
pin columns
jbrockmendel Aug 23, 2019
d867034
patch _default_fill_value
jbrockmendel Aug 23, 2019
ec3b4f5
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 23, 2019
94b5dbd
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 23, 2019
ca76be4
pin default_fill_value after align
jbrockmendel Aug 23, 2019
2620ef4
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Aug 27, 2019
4c836d3
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Sep 2, 2019
45f5419
improve docstring
jbrockmendel Sep 2, 2019
ceeb1d2
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Sep 3, 2019
ad53e94
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Sep 12, 2019
dcc8569
remove unnecessary combine_const, combine_match_columns, combine_matc…
jbrockmendel Sep 12, 2019
540320c
remove unnecessary combine_frame
jbrockmendel Sep 12, 2019
affe4b4
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Sep 12, 2019
9ad91a2
Merge branch 'master' of https://github.com/pandas-dev/pandas into bl…
jbrockmendel Sep 17, 2019
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
16 changes: 15 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
ABCIndexClass,
ABCMultiIndex,
ABCSeries,
ABCSparseDataFrame,
)
from pandas.core.dtypes.missing import isna, notna

Expand Down Expand Up @@ -5334,6 +5335,15 @@ def _combine_frame(self, other, func, fill_value=None, level=None):
# overhead if possible by doing this check once
_arith_op = func

elif isinstance(self, ABCSparseDataFrame):

def _arith_op(left, right):
# wrap the non-sparse case below with to_dense and to_sparse
dleft = left.to_dense()
dright = right.to_dense()
result = dleft._binop(dright, func, fill_value=fill_value)
return result.to_sparse(fill_value=left.fill_value)

else:

def _arith_op(left, right):
Expand All @@ -5356,7 +5366,11 @@ def _combine_match_index(self, other, func, level=None):
left, right = self.align(other, join="outer", axis=0, level=level, copy=False)
# at this point we have `left.index.equals(right.index)`

if left._is_mixed_type or right._is_mixed_type:
if (
left._is_mixed_type
or right._is_mixed_type
or isinstance(self, ABCSparseDataFrame)
):
# operate column-wise; avoid costly object-casting in `.values`
new_data = ops.dispatch_to_series(left, right, func)
else:
Expand Down
56 changes: 5 additions & 51 deletions pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,57 +533,10 @@ def _set_value(self, index, col, value, takeable=False):
# ----------------------------------------------------------------------
# Arithmetic-related methods

def _combine_frame(self, other, func, fill_value=None, level=None):
this, other = self.align(other, join="outer", level=level, copy=False)
def align(self, other, *args, **kwargs):
this, other = super().align(other, *args, **kwargs)
this._default_fill_value = self._default_fill_value

new_data = {}
if fill_value is not None:
# TODO: be a bit more intelligent here
for col in this.columns:
if col in this and col in other:
dleft = this[col].to_dense()
dright = other[col].to_dense()
result = dleft._binop(dright, func, fill_value=fill_value)
result = result.to_sparse(fill_value=this[col].fill_value)
new_data[col] = result
else:

for col in this.columns:
if col in this and col in other:
new_data[col] = func(this[col], other[col])

return this._construct_result(other, new_data, func)

def _combine_match_index(self, other, func, level=None):
this, other = self.align(other, join="outer", axis=0, level=level, copy=False)
this._default_fill_value = self._default_fill_value

new_data = {}
for col in this.columns:
new_data[col] = func(this[col], other)

return this._construct_result(other, new_data, func)

def _combine_match_columns(self, other, func, level=None):
# patched version of DataFrame._combine_match_columns to account for
# NumPy circumventing __rsub__ with float64 types, e.g.: 3.0 - series,
# where 3.0 is numpy.float64 and series is a SparseSeries. Still
# possible for this to happen, which is bothersome

left, right = self.align(other, join="outer", axis=1, level=level, copy=False)
assert left.columns.equals(right.index)
left._default_fill_value = self._default_fill_value

new_data = {}
for col in left.columns:
new_data[col] = func(left[col], right[col])

# TODO: using this changed some behavior, see GH#28025
return left._construct_result(other, new_data, func)

def _combine_const(self, other, func):
return self._apply_columns(lambda x: func(x, other))
return this, other

def _construct_result(self, other, result, func):
"""
Expand Down Expand Up @@ -639,7 +592,8 @@ def _get_op_result_fill_value(self, other, func):
fill_value = self.default_fill_value

else:
raise NotImplementedError(type(other))
# reached via _combine_const
fill_value = self.default_fill_value

return fill_value

Expand Down