Skip to content

DEPR: store SparseArray directly in Index #49307

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

Merged
merged 1 commit into from
Oct 25, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ Removal of prior version deprecations/changes
- Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`)
- Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`)
- Removed the deprecated method ``tshift`` from pandas classes (:issue:`11631`)
- Changed behavior of :class:`Index` constructor when passed a ``SparseArray`` or ``SparseDtype`` to retain that dtype instead of casting to ``numpy.ndarray`` (:issue:`43930`)

.. ---------------------------------------------------------------------------
.. _whatsnew_200.performance:
Expand Down
12 changes: 0 additions & 12 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@
tz_to_dtype,
validate_tz_from_dtype,
)
from pandas.core.arrays.sparse import SparseDtype
from pandas.core.arrays.string_ import StringArray
from pandas.core.base import (
IndexOpsMixin,
Expand Down Expand Up @@ -618,17 +617,6 @@ def _dtype_to_subclass(cls, dtype: DtypeObj):

return PeriodIndex

elif isinstance(dtype, SparseDtype):
warnings.warn(
"In a future version, passing a SparseArray to pd.Index "
"will store that array directly instead of converting to a "
"dense numpy ndarray. To retain the old behavior, use "
"pd.Index(arr.to_numpy()) instead",
FutureWarning,
stacklevel=find_stack_level(),
)
return cls._dtype_to_subclass(dtype.subtype)

return Index

if dtype.kind == "M":
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/base/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ def test_array_multiindex_raises():
def test_to_numpy(arr, expected, index_or_series_or_array, request):
box = index_or_series_or_array

warn = None
if index_or_series_or_array is pd.Index and isinstance(arr, SparseArray):
warn = FutureWarning
with tm.assert_produces_warning(warn):
with tm.assert_produces_warning(None):
thing = box(arr)

if arr.dtype.name == "int64" and box is pd.array:
Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,6 @@ def test_reindex(self, data, na_value):


class TestIndex(base.BaseIndexTests):
def test_index_from_array(self, data):
msg = "will store that array directly"
with tm.assert_produces_warning(FutureWarning, match=msg):
idx = pd.Index(data)

if data.dtype.subtype == "f":
assert idx.dtype == np.float64
elif data.dtype.subtype == "i":
assert idx.dtype == np.int64
else:
assert idx.dtype == data.dtype.subtype

# TODO(2.0): should pass once SparseArray is stored directly in Index.
@pytest.mark.xfail(reason="Index cannot yet store sparse dtype")
Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,9 @@ def test_constructor_from_sparse_array(self):
Timestamp("2016-05-01T01:00:00.000000"),
]
arr = pd.arrays.SparseArray(values)
msg = "will store that array directly"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = Index(arr)
expected = DatetimeIndex(values)
tm.assert_index_equal(result, expected)
result = Index(arr)
assert type(result) is Index
assert result.dtype == arr.dtype

def test_construction_caching(self):

Expand Down
28 changes: 5 additions & 23 deletions pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import numpy as np
import pytest

from pandas.core.dtypes.common import is_dtype_equal

import pandas as pd
import pandas._testing as tm
from pandas.arrays import SparseArray
Expand Down Expand Up @@ -277,14 +275,10 @@ def test_multiply(self, values_for_np_reduce, box_with_array, request):
box = box_with_array
values = values_for_np_reduce

warn = None
if is_dtype_equal(values.dtype, "Sparse[int]") and box is pd.Index:
warn = FutureWarning
msg = "passing a SparseArray to pd.Index"
with tm.assert_produces_warning(warn, match=msg):
with tm.assert_produces_warning(None):
obj = box(values)

if isinstance(values, pd.core.arrays.SparseArray) and box is not pd.Index:
if isinstance(values, pd.core.arrays.SparseArray):
mark = pytest.mark.xfail(reason="SparseArray has no 'prod'")
request.node.add_marker(mark)

Expand Down Expand Up @@ -316,11 +310,7 @@ def test_add(self, values_for_np_reduce, box_with_array):
box = box_with_array
values = values_for_np_reduce

warn = None
if is_dtype_equal(values.dtype, "Sparse[int]") and box is pd.Index:
warn = FutureWarning
msg = "passing a SparseArray to pd.Index"
with tm.assert_produces_warning(warn, match=msg):
with tm.assert_produces_warning(None):
obj = box(values)

if values.dtype.kind in "miuf":
Expand Down Expand Up @@ -355,11 +345,7 @@ def test_max(self, values_for_np_reduce, box_with_array):
# ATM Index casts to object, so we get python ints/floats
same_type = False

warn = None
if is_dtype_equal(values.dtype, "Sparse[int]") and box is pd.Index:
warn = FutureWarning
msg = "passing a SparseArray to pd.Index"
with tm.assert_produces_warning(warn, match=msg):
with tm.assert_produces_warning(None):
obj = box(values)

result = np.maximum.reduce(obj)
Expand All @@ -383,11 +369,7 @@ def test_min(self, values_for_np_reduce, box_with_array):
# ATM Index casts to object, so we get python ints/floats
same_type = False

warn = None
if is_dtype_equal(values.dtype, "Sparse[int]") and box is pd.Index:
warn = FutureWarning
msg = "passing a SparseArray to pd.Index"
with tm.assert_produces_warning(warn, match=msg):
with tm.assert_produces_warning(None):
obj = box(values)

result = np.minimum.reduce(obj)
Expand Down