Skip to content

CLN: missing boilerplate in Sparse op #27910

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
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
)
from pandas.core.dtypes.dtypes import register_extension_dtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCIndexClass,
ABCSeries,
ABCSparseArray,
Expand Down Expand Up @@ -1735,13 +1736,15 @@ def sparse_unary_method(self):

@classmethod
def _create_arithmetic_method(cls, op):
def sparse_arithmetic_method(self, other):
op_name = op.__name__
op_name = op.__name__

if isinstance(other, (ABCSeries, ABCIndexClass)):
def sparse_arithmetic_method(self, other):
if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):
# Rely on pandas to dispatch to us.
return NotImplemented

other = lib.item_from_zerodim(other)

if isinstance(other, SparseArray):
return _sparse_array_op(self, other, op, op_name)

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arrays/sparse/test_arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ def test_with_list(op):
tm.assert_sp_array_equal(result, expected)


def test_with_dataframe():
# GH#27910
arr = pd.SparseArray([0, 1], fill_value=0)
df = pd.DataFrame([[1, 2], [3, 4]])
result = arr.__add__(df)
assert result is NotImplemented


def test_with_zerodim_ndarray():
# GH#27910
arr = pd.SparseArray([0, 1], fill_value=0)

result = arr * np.array(2)
expected = arr * 2
tm.assert_sp_array_equal(result, expected)


@pytest.mark.parametrize("ufunc", [np.abs, np.exp])
@pytest.mark.parametrize(
"arr", [pd.SparseArray([0, 0, -1, 1]), pd.SparseArray([None, None, -1, 1])]
Expand Down