Skip to content

Commit 489d1ff

Browse files
jbrockmendelTomAugspurger
authored andcommitted
CLN: missing boilerplate in Sparse op (#27910)
* CLN: missing boilerplate in Sparse op
1 parent 6e0ab71 commit 489d1ff

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pandas/core/arrays/sparse.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040
from pandas.core.dtypes.dtypes import register_extension_dtype
4141
from pandas.core.dtypes.generic import (
42+
ABCDataFrame,
4243
ABCIndexClass,
4344
ABCSeries,
4445
ABCSparseArray,
@@ -1735,13 +1736,15 @@ def sparse_unary_method(self):
17351736

17361737
@classmethod
17371738
def _create_arithmetic_method(cls, op):
1738-
def sparse_arithmetic_method(self, other):
1739-
op_name = op.__name__
1739+
op_name = op.__name__
17401740

1741-
if isinstance(other, (ABCSeries, ABCIndexClass)):
1741+
def sparse_arithmetic_method(self, other):
1742+
if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):
17421743
# Rely on pandas to dispatch to us.
17431744
return NotImplemented
17441745

1746+
other = lib.item_from_zerodim(other)
1747+
17451748
if isinstance(other, SparseArray):
17461749
return _sparse_array_op(self, other, op, op_name)
17471750

pandas/tests/arrays/sparse/test_arithmetics.py

+17
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,23 @@ def test_with_list(op):
441441
tm.assert_sp_array_equal(result, expected)
442442

443443

444+
def test_with_dataframe():
445+
# GH#27910
446+
arr = pd.SparseArray([0, 1], fill_value=0)
447+
df = pd.DataFrame([[1, 2], [3, 4]])
448+
result = arr.__add__(df)
449+
assert result is NotImplemented
450+
451+
452+
def test_with_zerodim_ndarray():
453+
# GH#27910
454+
arr = pd.SparseArray([0, 1], fill_value=0)
455+
456+
result = arr * np.array(2)
457+
expected = arr * 2
458+
tm.assert_sp_array_equal(result, expected)
459+
460+
444461
@pytest.mark.parametrize("ufunc", [np.abs, np.exp])
445462
@pytest.mark.parametrize(
446463
"arr", [pd.SparseArray([0, 0, -1, 1]), pd.SparseArray([None, None, -1, 1])]

0 commit comments

Comments
 (0)