Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: index argument to SparseArray #43523

Merged
merged 2 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down Expand Up @@ -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")

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