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 all 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
2 changes: 2 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5826,6 +5826,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 "
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,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,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we dont want the trailing comma here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth rerunning CI over?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can change it in my next "assorted cleanups" PR


@property
def index(self):
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,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")
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]
7 changes: 6 additions & 1 deletion pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down