diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 9cfcd0adc92ec..67dc6ade25254 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -940,7 +940,7 @@ def set_categories( "a future version. Removing unused categories will always " "return a new Categorical object.", FutureWarning, - stacklevel=find_stack_level(), + stacklevel=2, ) else: inplace = False diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2cf8ea95a345a..f292316386f5e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -7016,7 +7016,7 @@ def _maybe_cast_data_without_dtype( "In a future version, the Index constructor will not infer numeric " "dtypes when passed object-dtype sequences (matching Series behavior)", FutureWarning, - stacklevel=find_stack_level(), + stacklevel=3, ) if result.dtype.kind in ["b", "c"]: return subarr diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 597216f55e444..d30b726966e3d 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -947,7 +947,7 @@ def test_setitem_mask_categorical(self): df = DataFrame({"cats": catsf, "values": valuesf}, index=idxf) exp_fancy = exp_multi_row.copy() - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): # issue #37643 inplace kwarg deprecated return_value = exp_fancy["cats"].cat.set_categories( ["a", "b", "c"], inplace=True diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index fb2071ac9c3f6..9dfb36a71c861 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -51,7 +51,7 @@ def test_cat_accessor(self): exp = Categorical(["a", "b", np.nan, "a"], categories=["b", "a"]) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): # issue #37643 inplace kwarg deprecated return_value = ser.cat.set_categories(["b", "a"], inplace=True)