Skip to content

Commit 01b86ed

Browse files
authored
ENH: Use find_stack_level in pandas.core (#44358)
1 parent 53e83c7 commit 01b86ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+169
-135
lines changed

pandas/core/accessor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import warnings
1010

1111
from pandas.util._decorators import doc
12+
from pandas.util._exceptions import find_stack_level
1213

1314

1415
class DirNamesMixin:
@@ -267,7 +268,7 @@ def decorator(accessor):
267268
f"{repr(name)} for type {repr(cls)} is overriding a preexisting "
268269
f"attribute with the same name.",
269270
UserWarning,
270-
stacklevel=2,
271+
stacklevel=find_stack_level(),
271272
)
272273
setattr(cls, name, CachedAccessor(name, accessor))
273274
cls._accessors.add(name)

pandas/core/arraylike.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212

1313
from pandas._libs import lib
14+
from pandas.util._exceptions import find_stack_level
1415

1516
from pandas.core.construction import extract_array
1617
from pandas.core.ops import (
@@ -210,7 +211,7 @@ def _maybe_fallback(ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any):
210211
"or align manually (eg 'df1, df2 = df1.align(df2)') before passing to "
211212
"the ufunc to obtain the future behaviour and silence this warning.",
212213
FutureWarning,
213-
stacklevel=4,
214+
stacklevel=find_stack_level(),
214215
)
215216

216217
# keep the first dataframe of the inputs, other DataFrame/Series is

pandas/core/arrays/categorical.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def __init__(
390390
"Allowing scalars in the Categorical constructor is deprecated "
391391
"and will raise in a future version. Use `[value]` instead",
392392
FutureWarning,
393-
stacklevel=2,
393+
stacklevel=find_stack_level(),
394394
)
395395
values = [values]
396396

@@ -945,7 +945,7 @@ def set_categories(
945945
"a future version. Removing unused categories will always "
946946
"return a new Categorical object.",
947947
FutureWarning,
948-
stacklevel=2,
948+
stacklevel=find_stack_level(),
949949
)
950950
else:
951951
inplace = False
@@ -1045,7 +1045,7 @@ def rename_categories(self, new_categories, inplace=no_default):
10451045
"a future version. Removing unused categories will always "
10461046
"return a new Categorical object.",
10471047
FutureWarning,
1048-
stacklevel=2,
1048+
stacklevel=find_stack_level(),
10491049
)
10501050
else:
10511051
inplace = False
@@ -1177,7 +1177,7 @@ def add_categories(self, new_categories, inplace=no_default):
11771177
"a future version. Removing unused categories will always "
11781178
"return a new Categorical object.",
11791179
FutureWarning,
1180-
stacklevel=2,
1180+
stacklevel=find_stack_level(),
11811181
)
11821182
else:
11831183
inplace = False
@@ -1252,7 +1252,7 @@ def remove_categories(self, removals, inplace=no_default):
12521252
"a future version. Removing unused categories will always "
12531253
"return a new Categorical object.",
12541254
FutureWarning,
1255-
stacklevel=2,
1255+
stacklevel=find_stack_level(),
12561256
)
12571257
else:
12581258
inplace = False
@@ -1327,7 +1327,7 @@ def remove_unused_categories(self, inplace=no_default):
13271327
"remove_unused_categories is deprecated and "
13281328
"will be removed in a future version.",
13291329
FutureWarning,
1330-
stacklevel=2,
1330+
stacklevel=find_stack_level(),
13311331
)
13321332
else:
13331333
inplace = False
@@ -1884,7 +1884,7 @@ def to_dense(self) -> np.ndarray:
18841884
"Categorical.to_dense is deprecated and will be removed in "
18851885
"a future version. Use np.asarray(cat) instead.",
18861886
FutureWarning,
1887-
stacklevel=2,
1887+
stacklevel=find_stack_level(),
18881888
)
18891889
return np.asarray(self)
18901890

@@ -1901,7 +1901,7 @@ def _codes(self, value: np.ndarray):
19011901
"Setting the codes on a Categorical is deprecated and will raise in "
19021902
"a future version. Create a new Categorical object instead",
19031903
FutureWarning,
1904-
stacklevel=2,
1904+
stacklevel=find_stack_level(),
19051905
) # GH#40606
19061906
NDArrayBacked.__init__(self, value, self.dtype)
19071907

@@ -1924,7 +1924,7 @@ def take_nd(self, indexer, allow_fill: bool = False, fill_value=None):
19241924
warn(
19251925
"Categorical.take_nd is deprecated, use Categorical.take instead",
19261926
FutureWarning,
1927-
stacklevel=2,
1927+
stacklevel=find_stack_level(),
19281928
)
19291929
return self.take(indexer, allow_fill=allow_fill, fill_value=fill_value)
19301930

@@ -2344,7 +2344,7 @@ def is_dtype_equal(self, other) -> bool:
23442344
"Categorical.is_dtype_equal is deprecated and will be removed "
23452345
"in a future version",
23462346
FutureWarning,
2347-
stacklevel=2,
2347+
stacklevel=find_stack_level(),
23482348
)
23492349
try:
23502350
return self._categories_match_up_to_permutation(other)

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ def to_perioddelta(self, freq) -> TimedeltaArray:
12061206
"Use `dtindex - dtindex.to_period(freq).to_timestamp()` instead.",
12071207
FutureWarning,
12081208
# stacklevel chosen to be correct for when called from DatetimeIndex
1209-
stacklevel=3,
1209+
stacklevel=find_stack_level(),
12101210
)
12111211
from pandas.core.arrays.timedeltas import TimedeltaArray
12121212

@@ -1373,7 +1373,7 @@ def weekofyear(self):
13731373
"weekofyear and return an Index, you may call "
13741374
"pd.Int64Index(idx.isocalendar().week)",
13751375
FutureWarning,
1376-
stacklevel=3,
1376+
stacklevel=find_stack_level(),
13771377
)
13781378
week_series = self.isocalendar().week
13791379
if week_series.hasnans:

pandas/core/arrays/sparse/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def __init__(
467467
"loses timezone information. Cast to object before "
468468
"sparse to retain timezone information.",
469469
UserWarning,
470-
stacklevel=2,
470+
stacklevel=find_stack_level(),
471471
)
472472
data = np.asarray(data, dtype="datetime64[ns]")
473473
if fill_value is NaT:
@@ -1089,7 +1089,7 @@ def searchsorted(
10891089
) -> npt.NDArray[np.intp] | np.intp:
10901090

10911091
msg = "searchsorted requires high memory usage."
1092-
warnings.warn(msg, PerformanceWarning, stacklevel=2)
1092+
warnings.warn(msg, PerformanceWarning, stacklevel=find_stack_level())
10931093
if not is_scalar(v):
10941094
v = np.asarray(v)
10951095
v = np.asarray(v)

pandas/core/arrays/sparse/dtype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
type_t,
1717
)
1818
from pandas.errors import PerformanceWarning
19+
from pandas.util._exceptions import find_stack_level
1920

2021
from pandas.core.dtypes.base import (
2122
ExtensionDtype,
@@ -389,7 +390,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
389390
f"values: '{fill_values}'. Picking the first and "
390391
"converting the rest.",
391392
PerformanceWarning,
392-
stacklevel=6,
393+
stacklevel=find_stack_level(),
393394
)
394395

395396
np_dtypes = [x.subtype if isinstance(x, SparseDtype) else x for x in dtypes]

pandas/core/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
Scalar,
3737
T,
3838
)
39+
from pandas.util._exceptions import find_stack_level
3940

4041
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
4142
from pandas.core.dtypes.common import (
@@ -175,7 +176,7 @@ def cast_scalar_indexer(val, warn_float: bool = False):
175176
"Indexing with a float is deprecated, and will raise an IndexError "
176177
"in pandas 2.0. You can manually convert to an integer key instead.",
177178
FutureWarning,
178-
stacklevel=3,
179+
stacklevel=find_stack_level(),
179180
)
180181
return int(val)
181182
return val

pandas/core/computation/align.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import numpy as np
1717

1818
from pandas.errors import PerformanceWarning
19+
from pandas.util._exceptions import find_stack_level
1920

2021
from pandas.core.dtypes.generic import (
2122
ABCDataFrame,
@@ -126,7 +127,9 @@ def _align_core(terms):
126127
f"than an order of magnitude on term {repr(terms[i].name)}, "
127128
f"by more than {ordm:.4g}; performance may suffer."
128129
)
129-
warnings.warn(w, category=PerformanceWarning, stacklevel=6)
130+
warnings.warn(
131+
w, category=PerformanceWarning, stacklevel=find_stack_level()
132+
)
130133

131134
f = partial(ti.reindex, reindexer, axis=axis, copy=False)
132135

pandas/core/computation/eval.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import warnings
88

99
from pandas._libs.lib import no_default
10+
from pandas.util._exceptions import find_stack_level
1011
from pandas.util._validators import validate_bool_kwarg
1112

1213
from pandas.core.computation.engines import ENGINES
@@ -308,7 +309,7 @@ def eval(
308309
"will be removed in a future version."
309310
),
310311
FutureWarning,
311-
stacklevel=2,
312+
stacklevel=find_stack_level(),
312313
)
313314

314315
exprs: list[str | BinOp]

pandas/core/config_init.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
is_text,
2626
)
2727

28+
from pandas.util._exceptions import find_stack_level
29+
2830
# compute
2931

3032
use_bottleneck_doc = """
@@ -373,7 +375,7 @@ def _deprecate_negative_int_max_colwidth(key):
373375
"will not be supported in future version. Instead, use None "
374376
"to not limit the column width.",
375377
FutureWarning,
376-
stacklevel=4,
378+
stacklevel=find_stack_level(),
377379
)
378380

379381
cf.register_option(

0 commit comments

Comments
 (0)