Skip to content

Commit c0c1c9a

Browse files
jbrockmendeljreback
authored andcommitted
check early for non-scalar default_fill_value (#27302)
1 parent 134bec4 commit c0c1c9a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/core/sparse/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import numpy as np
88

9+
from pandas._libs.lib import is_scalar, item_from_zerodim
910
from pandas._libs.sparse import BlockIndex, get_blocks
1011
from pandas.compat.numpy import function as nv
1112
from pandas.util._decorators import Appender
@@ -74,6 +75,8 @@ def __init__(
7475
dtype=None,
7576
copy=False,
7677
):
78+
if not is_scalar(default_fill_value):
79+
raise ValueError("'default_fill_value' must be a scalar")
7780

7881
warnings.warn(depr_msg, FutureWarning, stacklevel=2)
7982
# pick up the defaults from the Sparse structures
@@ -666,7 +669,7 @@ def _get_op_result_fill_value(self, other, func):
666669
fill_value = np.nan
667670
else:
668671
fill_value = func(np.float64(own_default), np.float64(other.fill_value))
669-
672+
fill_value = item_from_zerodim(fill_value)
670673
else:
671674
raise NotImplementedError(type(other))
672675

pandas/tests/sparse/frame/test_frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ def test_constructor(self, float_frame, float_frame_int_kind, float_frame_fill0)
136136

137137
repr(float_frame)
138138

139+
def test_constructor_fill_value_not_scalar_raises(self):
140+
d = {"b": [2, 3], "a": [0, 1]}
141+
fill_value = np.array(np.nan)
142+
with pytest.raises(ValueError, match="must be a scalar"):
143+
SparseDataFrame(data=d, default_fill_value=fill_value)
144+
139145
def test_constructor_dict_order(self):
140146
# GH19018
141147
# initialization ordering: by insertion order if python>= 3.6, else

0 commit comments

Comments
 (0)