Skip to content

Commit 57dfb14

Browse files
committed
DEPR: Deprecate n-dim indexing for Series
Closes pandas-dev#27837
1 parent fdaa5e4 commit 57dfb14

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ Deprecations
788788
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)
789789
- The ``fastpath`` keyword in the ``SingleBlockManager`` constructor is deprecated and will be removed in a future version (:issue:`33092`)
790790
- Providing ``suffixes`` as a ``set`` in :func:`pandas.merge` is deprecated. Provide a tuple instead (:issue:`33740`, :issue:`34741`).
791+
- 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`)
791792
- :meth:`Index.is_mixed` is deprecated and will be removed in a future version, check ``index.inferred_type`` directly instead (:issue:`32922`)
792793

793794
- 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+
"on 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/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)