Skip to content

Commit 0575149

Browse files
Avoid Index DeprecationWarning in Series getitem (#31361)
* Avoid Index DeprecationWarning in Series getitem xref #30867
1 parent 511fd46 commit 0575149

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
@@ -5832,6 +5832,8 @@ def deprecate_ndim_indexing(result):
58325832
# GH#27125 indexer like idx[:, None] expands dim, but we
58335833
# cannot do that and keep an index, so return ndarray
58345834
# Deprecation GH#30588
5835+
# Note: update SingleBlockManager.get_slice when the DeprecationWarning
5836+
# is elevated to a FutureWarning
58355837
warnings.warn(
58365838
"Support for multi-dimensional indexing (e.g. `index[:, None]`) "
58375839
"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
@@ -1505,7 +1505,7 @@ def get_slice(self, slobj, axis=0):
15051505
if axis >= self.ndim:
15061506
raise IndexError("Requested axis not found in manager")
15071507

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

15101510
@property
15111511
def index(self):

pandas/core/series.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,13 @@ def _get_with(self, key):
940940
def _get_values_tuple(self, key):
941941
# mpl hackaround
942942
if com.any_none(*key):
943-
return self._get_values(key)
943+
# suppress warning from slicing the index with a 2d indexer.
944+
# eventually we'll want Series itself to warn.
945+
with warnings.catch_warnings():
946+
warnings.filterwarnings(
947+
"ignore", "Support for multi-dim", DeprecationWarning
948+
)
949+
return self._get_values(key)
944950

945951
if not isinstance(self.index, MultiIndex):
946952
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)