diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 328499a4ae98e..ff8f8b9977134 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -282,6 +282,8 @@ Other Deprecations - Deprecated passing in a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`) - 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`) - 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`) +- Deprecated the ``index`` argument to :class:`SparseArray` construction (:issue:`23089`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 77142ef450487..5446b4584f4e9 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -41,6 +41,7 @@ ) from pandas.compat.numpy import function as nv from pandas.errors import PerformanceWarning +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.cast import ( astype_nansafe, @@ -256,6 +257,11 @@ class SparseArray(OpsMixin, PandasObject, ExtensionArray): `fill_value`. sparse_index : SparseIndex, optional index : Index + + .. deprecated:: 1.4.0 + Use a function like `np.full` to construct an array with the desired + repeats of the scalar value instead. + fill_value : scalar, optional Elements in `data` that are `fill_value` are not stored in the SparseArray. For memory savings, this should be the most common value @@ -359,6 +365,16 @@ def __init__( fill_value = dtype.fill_value dtype = dtype.subtype + if index is not None: + warnings.warn( + "The index argument has been deprecated and will be " + "removed in a future version. Use a function like np.full " + "to construct an array with the desired repeats of the " + "scalar value instead.\n\n", + FutureWarning, + stacklevel=find_stack_level(), + ) + if index is not None and not is_scalar(data): raise Exception("must only pass scalars with an index") diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index fbbe521b0eaf1..c476afb97446e 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -237,7 +237,10 @@ def test_from_spmatrix_raises(self): ) def test_scalar_with_index_infer_dtype(self, scalar, dtype): # GH 19163 - arr = SparseArray(scalar, index=[1, 2, 3], fill_value=scalar) + with tm.assert_produces_warning( + FutureWarning, match="The index argument has been deprecated" + ): + arr = SparseArray(scalar, index=[1, 2, 3], fill_value=scalar) exp = SparseArray([scalar, scalar, scalar], fill_value=scalar) tm.assert_sp_array_equal(arr, exp)