Skip to content

Avoid Index DeprecationWarning in Series getitem #31361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4168,6 +4168,13 @@ def __setitem__(self, key, value):
raise TypeError("Index does not support mutable operations")

def __getitem__(self, key):
# To support the deprecation of Index[:, None] returning an
# ndarray with a warning, we use a helper method _getitem_deprecate_nd.
# Series[:, None] eventaully calls that with warn=False, via
# SingleBlockManager.get_slice
return self._getitem_deprecate_nd(key)

def _getitem_deprecate_nd(self, key, warn=True):
"""
Override numpy.ndarray's __getitem__ method to work as desired.

Expand Down Expand Up @@ -4199,7 +4206,8 @@ def __getitem__(self, key):
result = getitem(key)
if not is_scalar(result):
if np.ndim(result) > 1:
deprecate_ndim_indexing(result)
if warn:
deprecate_ndim_indexing(result)
return result
return promote(result)
else:
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,11 @@ 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._getitem_deprecate_nd(slobj, warn=False),
fastpath=True,
)

@property
def index(self):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]