Skip to content

Commit 85c02bc

Browse files
DEPR: Deprecate n-dim indexing for Series (#35141)
1 parent 8590bd5 commit 85c02bc

File tree

8 files changed

+23
-24
lines changed

8 files changed

+23
-24
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ Deprecations
789789
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)
790790
- The ``fastpath`` keyword in the ``SingleBlockManager`` constructor is deprecated and will be removed in a future version (:issue:`33092`)
791791
- Providing ``suffixes`` as a ``set`` in :func:`pandas.merge` is deprecated. Provide a tuple instead (:issue:`33740`, :issue:`34741`).
792+
- Indexing a series with a multi-dimensional indexer like ``[:, None]`` to return an ndarray now raises a ``FutureWarning``. Convert to a NumPy array before indexing instead (:issue:`27837`)
792793
- :meth:`Index.is_mixed` is deprecated and will be removed in a future version, check ``index.inferred_type`` directly instead (:issue:`32922`)
793794

794795
- Passing any arguments but the first one to :func:`read_html` as

pandas/core/indexers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def length_of_indexer(indexer, target=None) -> int:
295295
raise AssertionError("cannot find the length of the indexer")
296296

297297

298-
def deprecate_ndim_indexing(result):
298+
def deprecate_ndim_indexing(result, stacklevel=3):
299299
"""
300300
Helper function to raise the deprecation warning for multi-dimensional
301301
indexing on 1D Series/Index.
@@ -306,11 +306,11 @@ def deprecate_ndim_indexing(result):
306306
"""
307307
if np.ndim(result) > 1:
308308
warnings.warn(
309-
"Support for multi-dimensional indexing (e.g. `index[:, None]`) "
310-
"on an Index is deprecated and will be removed in a future "
309+
"Support for multi-dimensional indexing (e.g. `obj[:, None]`) "
310+
"is deprecated and will be removed in a future "
311311
"version. Convert to a numpy array before indexing instead.",
312-
DeprecationWarning,
313-
stacklevel=3,
312+
FutureWarning,
313+
stacklevel=stacklevel,
314314
)
315315

316316

pandas/core/series.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
sanitize_array,
7979
)
8080
from pandas.core.generic import NDFrame
81-
from pandas.core.indexers import unpack_1tuple
81+
from pandas.core.indexers import deprecate_ndim_indexing, unpack_1tuple
8282
from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
8383
from pandas.core.indexes.api import Float64Index, Index, MultiIndex, ensure_index
8484
import pandas.core.indexes.base as ibase
@@ -950,13 +950,9 @@ def _get_with(self, key):
950950
def _get_values_tuple(self, key):
951951
# mpl hackaround
952952
if com.any_none(*key):
953-
# suppress warning from slicing the index with a 2d indexer.
954-
# eventually we'll want Series itself to warn.
955-
with warnings.catch_warnings():
956-
warnings.filterwarnings(
957-
"ignore", "Support for multi-dim", DeprecationWarning
958-
)
959-
return self._get_values(key)
953+
result = self._get_values(key)
954+
deprecate_ndim_indexing(result, stacklevel=5)
955+
return result
960956

961957
if not isinstance(self.index, MultiIndex):
962958
raise ValueError("Can only tuple-index with a MultiIndex")

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def test_engine_reference_cycle(self):
855855
def test_getitem_2d_deprecated(self):
856856
# GH#30588
857857
idx = self.create_index()
858-
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
858+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
859859
res = idx[:, None]
860860

861861
assert isinstance(res, np.ndarray), type(res)

pandas/tests/indexes/datetimes/test_indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_dti_business_getitem(self):
9595

9696
def test_dti_business_getitem_matplotlib_hackaround(self):
9797
rng = pd.bdate_range(START, END)
98-
with tm.assert_produces_warning(DeprecationWarning):
98+
with tm.assert_produces_warning(FutureWarning):
9999
# GH#30588 multi-dimensional indexing deprecated
100100
values = rng[:, None]
101101
expected = rng.values[:, None]
@@ -122,7 +122,7 @@ def test_dti_custom_getitem(self):
122122

123123
def test_dti_custom_getitem_matplotlib_hackaround(self):
124124
rng = pd.bdate_range(START, END, freq="C")
125-
with tm.assert_produces_warning(DeprecationWarning):
125+
with tm.assert_produces_warning(FutureWarning):
126126
# GH#30588 multi-dimensional indexing deprecated
127127
values = rng[:, None]
128128
expected = rng.values[:, None]

pandas/tests/indexes/interval/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ def test_getitem_2d_deprecated(self):
8484
# GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
8585
idx = self.create_index()
8686
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
87-
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
87+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
8888
idx[:, None]

pandas/tests/indexes/test_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_can_hold_identifiers(self):
5656

5757
@pytest.mark.parametrize("index", ["datetime"], indirect=True)
5858
def test_new_axis(self, index):
59-
with tm.assert_produces_warning(DeprecationWarning):
59+
with tm.assert_produces_warning(FutureWarning):
6060
# GH#30588 multi-dimensional indexing deprecated
6161
new_index = index[None, :]
6262
assert new_index.ndim == 2
@@ -2531,7 +2531,7 @@ def test_shape_of_invalid_index():
25312531
# that the returned shape is consistent with this underlying array for
25322532
# compat with matplotlib (see https://github.com/pandas-dev/pandas/issues/27775)
25332533
idx = pd.Index([0, 1, 2, 3])
2534-
with tm.assert_produces_warning(DeprecationWarning):
2534+
with tm.assert_produces_warning(FutureWarning):
25352535
# GH#30588 multi-dimensional indexing deprecated
25362536
assert idx[:, None].shape == (4, 1)
25372537

pandas/tests/series/indexing/test_getitem.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ class TestSeriesGetitemSlices:
5151
def test_getitem_slice_2d(self, datetime_series):
5252
# GH#30588 multi-dimensional indexing deprecated
5353

54-
# This is currently failing because the test was relying on
55-
# the DeprecationWarning coming through Index.__getitem__.
56-
# We want to implement a warning specifically for Series.__getitem__
57-
# at which point this will become a Deprecation/FutureWarning
58-
with tm.assert_produces_warning(None):
54+
with tm.assert_produces_warning(FutureWarning):
5955
# GH#30867 Don't want to support this long-term, but
6056
# for now ensure that the warning from Index
6157
# doesn't comes through via Series.__getitem__.
@@ -135,3 +131,9 @@ def test_getitem_generator(string_series):
135131
expected = string_series[string_series > 0]
136132
tm.assert_series_equal(result, expected)
137133
tm.assert_series_equal(result2, expected)
134+
135+
136+
def test_getitem_ndim_deprecated():
137+
s = pd.Series([0, 1])
138+
with tm.assert_produces_warning(FutureWarning):
139+
s[:, None]

0 commit comments

Comments
 (0)