diff --git a/pandas/core/indexers.py b/pandas/core/indexers.py index ed4b1a3fbb39c..ef9b48911dfa1 100644 --- a/pandas/core/indexers.py +++ b/pandas/core/indexers.py @@ -13,6 +13,7 @@ AnyArrayLike, ArrayLike, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_array_like, @@ -375,7 +376,7 @@ def deprecate_ndim_indexing(result, stacklevel: int = 3): "is deprecated and will be removed in a future " "version. Convert to a numpy array before indexing instead.", FutureWarning, - stacklevel=stacklevel, + stacklevel=find_stack_level(), ) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index cef756b709f70..54050e4a49b84 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -724,13 +724,10 @@ def test_engine_reference_cycle(self, simple_index): assert len(gc.get_referrers(index)) == nrefs_pre def test_getitem_2d_deprecated(self, simple_index): - # GH#30588 + # GH#30588, GH#31479 idx = simple_index msg = "Support for multi-dimensional indexing" - check = not isinstance(idx, (RangeIndex, CategoricalIndex)) - with tm.assert_produces_warning( - FutureWarning, match=msg, check_stacklevel=check - ): + with tm.assert_produces_warning(FutureWarning, match=msg): res = idx[:, None] assert isinstance(res, np.ndarray), type(res)