diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6f9c865db673b..c3839c9e09743 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5563,6 +5563,8 @@ def deprecate_ndim_indexing(result): # GH#27125 indexer like idx[:, None] expands dim, but we # cannot do that and keep an index, so return ndarray # Deprecation GH#30588 + # Note: update SingleBlockManager.get_slice when the DeprecationWarning + # is elevated to a FutureWarning warnings.warn( "Support for multi-dimensional indexing (e.g. `index[:, None]`) " "on an Index is deprecated and will be removed in a future " diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 066689b3e374e..67afd069b5ed1 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1539,7 +1539,7 @@ def get_slice(self, slobj, axis=0): if axis >= self.ndim: raise IndexError("Requested axis not found in manager") - return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True) + return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True,) @property def index(self): diff --git a/pandas/core/series.py b/pandas/core/series.py index 1e0760418d444..6a2a30a3efa17 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -962,7 +962,13 @@ def _get_with(self, key): def _get_values_tuple(self, key): # mpl hackaround if com.any_none(*key): - return self._get_values(key) + # suppress warning from slicing the index with a 2d indexer. + # eventually we'll want Series itself to warn. + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", "Support for multi-dim", DeprecationWarning + ) + return self._get_values(key) if not isinstance(self.index, MultiIndex): raise ValueError("Can only tuple-index with a MultiIndex") diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 6fd3d891c310b..4fa70793e2815 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -925,3 +925,13 @@ def test_uint_drop(any_int_dtype): series.loc[0] = 4 expected = pd.Series([4, 2, 3], dtype=any_int_dtype) tm.assert_series_equal(series, expected) + + +def test_getitem_2d_no_warning(): + # https://github.com/pandas-dev/pandas/issues/30867 + # Don't want to support this long-term, but + # for now ensure that the warning from Index + # doesn't comes through via Series.__getitem__. + series = pd.Series([1, 2, 3], index=[1, 2, 3]) + with tm.assert_produces_warning(None): + series[:, None] diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py index a2d14f27d7b7a..459377fb18f29 100644 --- a/pandas/tests/series/test_timeseries.py +++ b/pandas/tests/series/test_timeseries.py @@ -137,7 +137,12 @@ def test_first_last_valid(self, datetime_series): assert ts.last_valid_index().freq == ts.index.freq def test_mpl_compat_hack(self, datetime_series): - with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): + + # This is currently failing because the test was relying on + # the DeprecationWarning coming through Index.__getitem__. + # We want to implement a warning specifically for Series.__getitem__ + # at which point this will become a Deprecation/FutureWarning + with tm.assert_produces_warning(None): # GH#30588 multi-dimensional indexing deprecated result = datetime_series[:, np.newaxis] expected = datetime_series.values[:, np.newaxis]