Skip to content

Commit 4b232a2

Browse files
committed
Avoid Index DeprecationWarning in Series getitem
xref pandas-dev#30867
1 parent 5c37a0b commit 4b232a2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

pandas/core/indexes/base.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,13 @@ def __setitem__(self, key, value):
41684168
raise TypeError("Index does not support mutable operations")
41694169

41704170
def __getitem__(self, key):
4171+
# To support the deprecation of Index[:, None] returning an
4172+
# ndarray with a warning, we use a helper method _getitem_deprecate_nd.
4173+
# Series[:, None] eventaully calls that with warn=False, via
4174+
# SingleBlockManager.get_slice
4175+
return self._getitem_deprecate_nd(key)
4176+
4177+
def _getitem_deprecate_nd(self, key, warn=True):
41714178
"""
41724179
Override numpy.ndarray's __getitem__ method to work as desired.
41734180
@@ -4199,7 +4206,8 @@ def __getitem__(self, key):
41994206
result = getitem(key)
42004207
if not is_scalar(result):
42014208
if np.ndim(result) > 1:
4202-
deprecate_ndim_indexing(result)
4209+
if warn:
4210+
deprecate_ndim_indexing(result)
42034211
return result
42044212
return promote(result)
42054213
else:

pandas/core/internals/managers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,11 @@ 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)(
1509+
self._block._slice(slobj),
1510+
self.index._getitem_deprecate_nd(slobj, warn=False),
1511+
fastpath=True,
1512+
)
15091513

15101514
@property
15111515
def index(self):

pandas/tests/series/indexing/test_indexing.py

+9
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,12 @@ 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+
# Unclear that we want to support this long-term, but
933+
# for now ensure that no warning from Index comes through.
934+
series = pd.Series([1, 2, 3], index=[1, 2, 3])
935+
with tm.assert_produces_warning(None):
936+
series[:, None]

0 commit comments

Comments
 (0)