Skip to content

Commit

Permalink
DEPR: index argument to SparseArray (#43523)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeitlin11 committed Sep 12, 2021
1 parent 1c6c767 commit e1c7151
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

.. ---------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -258,6 +259,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
Expand Down Expand Up @@ -361,6 +367,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")

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e1c7151

Please sign in to comment.