Skip to content

Commit e1c7151

Browse files
authored
DEPR: index argument to SparseArray (#43523)
1 parent 1c6c767 commit e1c7151

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ Other Deprecations
282282
- Deprecated passing in a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
283283
- Deprecated the 'include_start' and 'include_end' arguments in :meth:`DataFrame.between_time`; in a future version passing 'include_start' or 'include_end' will raise (:issue:`40245`)
284284
- Deprecated the ``squeeze`` argument to :meth:`read_csv`, :meth:`read_table`, and :meth:`read_excel`. Users should squeeze the DataFrame afterwards with ``.squeeze("columns")`` instead. (:issue:`43242`)
285+
- Deprecated the ``index`` argument to :class:`SparseArray` construction (:issue:`23089`)
286+
-
285287

286288
.. ---------------------------------------------------------------------------
287289

pandas/core/arrays/sparse/array.py

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from pandas.compat.numpy import function as nv
4343
from pandas.errors import PerformanceWarning
44+
from pandas.util._exceptions import find_stack_level
4445

4546
from pandas.core.dtypes.cast import (
4647
astype_nansafe,
@@ -258,6 +259,11 @@ class SparseArray(OpsMixin, PandasObject, ExtensionArray):
258259
`fill_value`.
259260
sparse_index : SparseIndex, optional
260261
index : Index
262+
263+
.. deprecated:: 1.4.0
264+
Use a function like `np.full` to construct an array with the desired
265+
repeats of the scalar value instead.
266+
261267
fill_value : scalar, optional
262268
Elements in `data` that are `fill_value` are not stored in the
263269
SparseArray. For memory savings, this should be the most common value
@@ -361,6 +367,16 @@ def __init__(
361367
fill_value = dtype.fill_value
362368
dtype = dtype.subtype
363369

370+
if index is not None:
371+
warnings.warn(
372+
"The index argument has been deprecated and will be "
373+
"removed in a future version. Use a function like np.full "
374+
"to construct an array with the desired repeats of the "
375+
"scalar value instead.\n\n",
376+
FutureWarning,
377+
stacklevel=find_stack_level(),
378+
)
379+
364380
if index is not None and not is_scalar(data):
365381
raise Exception("must only pass scalars with an index")
366382

pandas/tests/arrays/sparse/test_array.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ def test_from_spmatrix_raises(self):
237237
)
238238
def test_scalar_with_index_infer_dtype(self, scalar, dtype):
239239
# GH 19163
240-
arr = SparseArray(scalar, index=[1, 2, 3], fill_value=scalar)
240+
with tm.assert_produces_warning(
241+
FutureWarning, match="The index argument has been deprecated"
242+
):
243+
arr = SparseArray(scalar, index=[1, 2, 3], fill_value=scalar)
241244
exp = SparseArray([scalar, scalar, scalar], fill_value=scalar)
242245

243246
tm.assert_sp_array_equal(arr, exp)

0 commit comments

Comments
 (0)