Skip to content

Commit a08c2f9

Browse files
Backport PR #31361: Avoid Index DeprecationWarning in Series getitem (#31403)
Co-authored-by: Tom Augspurger <[email protected]>
1 parent af208b3 commit a08c2f9

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

pandas/core/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -5563,6 +5563,8 @@ def deprecate_ndim_indexing(result):
55635563
# GH#27125 indexer like idx[:, None] expands dim, but we
55645564
# cannot do that and keep an index, so return ndarray
55655565
# Deprecation GH#30588
5566+
# Note: update SingleBlockManager.get_slice when the DeprecationWarning
5567+
# is elevated to a FutureWarning
55665568
warnings.warn(
55675569
"Support for multi-dimensional indexing (e.g. `index[:, None]`) "
55685570
"on an Index is deprecated and will be removed in a future "

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ def get_slice(self, slobj, axis=0):
15391539
if axis >= self.ndim:
15401540
raise IndexError("Requested axis not found in manager")
15411541

1542-
return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True)
1542+
return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True,)
15431543

15441544
@property
15451545
def index(self):

pandas/core/series.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,13 @@ def _get_with(self, key):
962962
def _get_values_tuple(self, key):
963963
# mpl hackaround
964964
if com.any_none(*key):
965-
return self._get_values(key)
965+
# suppress warning from slicing the index with a 2d indexer.
966+
# eventually we'll want Series itself to warn.
967+
with warnings.catch_warnings():
968+
warnings.filterwarnings(
969+
"ignore", "Support for multi-dim", DeprecationWarning
970+
)
971+
return self._get_values(key)
966972

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

pandas/tests/series/indexing/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,13 @@ def test_uint_drop(any_int_dtype):
925925
series.loc[0] = 4
926926
expected = pd.Series([4, 2, 3], dtype=any_int_dtype)
927927
tm.assert_series_equal(series, expected)
928+
929+
930+
def test_getitem_2d_no_warning():
931+
# https://github.com/pandas-dev/pandas/issues/30867
932+
# Don't want to support this long-term, but
933+
# for now ensure that the warning from Index
934+
# doesn't comes through via Series.__getitem__.
935+
series = pd.Series([1, 2, 3], index=[1, 2, 3])
936+
with tm.assert_produces_warning(None):
937+
series[:, None]

pandas/tests/series/test_timeseries.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ def test_first_last_valid(self, datetime_series):
137137
assert ts.last_valid_index().freq == ts.index.freq
138138

139139
def test_mpl_compat_hack(self, datetime_series):
140-
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
140+
141+
# This is currently failing because the test was relying on
142+
# the DeprecationWarning coming through Index.__getitem__.
143+
# We want to implement a warning specifically for Series.__getitem__
144+
# at which point this will become a Deprecation/FutureWarning
145+
with tm.assert_produces_warning(None):
141146
# GH#30588 multi-dimensional indexing deprecated
142147
result = datetime_series[:, np.newaxis]
143148
expected = datetime_series.values[:, np.newaxis]

0 commit comments

Comments
 (0)