Skip to content

Commit e0adf08

Browse files
jbrockmendelmroeschke
authored and
im-vinicius
committed
DEPR: SparseArray(scalar) (pandas-dev#53039)
* DEPR: SparseArray(scalar) * Update doc/source/whatsnew/v2.1.0.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 72cdb74 commit e0adf08

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ Deprecations
259259
- Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`)
260260
- Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`)
261261
- Deprecated logical operation between two non boolean :class:`Series` with different indexes always coercing the result to bool dtype. In a future version, this will maintain the return type of the inputs. (:issue:`52500`, :issue:`52538`)
262+
- Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`)
263+
-
262264

263265
.. ---------------------------------------------------------------------------
264266
.. _whatsnew_210.performance:

pandas/core/arrays/sparse/array.py

+6
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ def __init__(
398398
dtype = dtype.subtype
399399

400400
if is_scalar(data):
401+
warnings.warn(
402+
f"Constructing {type(self).__name__} with scalar data is deprecated "
403+
"and will raise in a future version. Pass a sequence instead.",
404+
FutureWarning,
405+
stacklevel=find_stack_level(),
406+
)
401407
if sparse_index is None:
402408
npoints = 1
403409
else:

pandas/tests/arrays/sparse/test_constructors.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,16 @@ def test_constructor_spindex_dtype(self):
145145
@pytest.mark.parametrize("sparse_index", [None, IntIndex(1, [0])])
146146
def test_constructor_spindex_dtype_scalar(self, sparse_index):
147147
# scalar input
148-
arr = SparseArray(data=1, sparse_index=sparse_index, dtype=None)
148+
msg = "Constructing SparseArray with scalar data is deprecated"
149+
with tm.assert_produces_warning(FutureWarning, match=msg):
150+
arr = SparseArray(data=1, sparse_index=sparse_index, dtype=None)
149151
exp = SparseArray([1], dtype=None)
150152
tm.assert_sp_array_equal(arr, exp)
151153
assert arr.dtype == SparseDtype(np.int64)
152154
assert arr.fill_value == 0
153155

154-
arr = SparseArray(data=1, sparse_index=IntIndex(1, [0]), dtype=None)
156+
with tm.assert_produces_warning(FutureWarning, match=msg):
157+
arr = SparseArray(data=1, sparse_index=IntIndex(1, [0]), dtype=None)
155158
exp = SparseArray([1], dtype=None)
156159
tm.assert_sp_array_equal(arr, exp)
157160
assert arr.dtype == SparseDtype(np.int64)

0 commit comments

Comments
 (0)