Skip to content

Commit 2f8d0a3

Browse files
authored
PERF cache find_stack_level (#48023)
cache stacklevel
1 parent 926b9ce commit 2f8d0a3

Some content is hidden

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

76 files changed

+384
-257
lines changed

pandas/_libs/interval.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
import numbers
23
from operator import (
34
le,
@@ -45,6 +46,7 @@ cnp.import_array()
4546
import warnings
4647

4748
from pandas._libs import lib
49+
4850
from pandas._libs cimport util
4951
from pandas._libs.hashtable cimport Int64Vector
5052
from pandas._libs.tslibs.timedeltas cimport _Timedelta
@@ -394,7 +396,7 @@ cdef class Interval(IntervalMixin):
394396
warnings.warn(
395397
"Attribute `closed` is deprecated in favor of `inclusive`.",
396398
FutureWarning,
397-
stacklevel=find_stack_level(),
399+
stacklevel=find_stack_level(inspect.currentframe()),
398400
)
399401
return self.inclusive
400402

pandas/_testing/asserters.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import inspect
34
from typing import (
45
Literal,
56
cast,
@@ -112,7 +113,7 @@ def assert_almost_equal(
112113
"is deprecated and will be removed in a future version. "
113114
"You can stop passing 'check_less_precise' to silence this warning.",
114115
FutureWarning,
115-
stacklevel=find_stack_level(),
116+
stacklevel=find_stack_level(inspect.currentframe()),
116117
)
117118
rtol = atol = _get_tol_from_less_precise(check_less_precise)
118119

@@ -339,7 +340,7 @@ def _get_ilevel_values(index, level):
339340
"is deprecated and will be removed in a future version. "
340341
"You can stop passing 'check_less_precise' to silence this warning.",
341342
FutureWarning,
342-
stacklevel=find_stack_level(),
343+
stacklevel=find_stack_level(inspect.currentframe()),
343344
)
344345
rtol = atol = _get_tol_from_less_precise(check_less_precise)
345346

@@ -815,7 +816,7 @@ def assert_extension_array_equal(
815816
"is deprecated and will be removed in a future version. "
816817
"You can stop passing 'check_less_precise' to silence this warning.",
817818
FutureWarning,
818-
stacklevel=find_stack_level(),
819+
stacklevel=find_stack_level(inspect.currentframe()),
819820
)
820821
rtol = atol = _get_tol_from_less_precise(check_less_precise)
821822

@@ -970,7 +971,7 @@ def assert_series_equal(
970971
"is deprecated and will be removed in a future version. "
971972
"You can stop passing 'check_less_precise' to silence this warning.",
972973
FutureWarning,
973-
stacklevel=find_stack_level(),
974+
stacklevel=find_stack_level(inspect.currentframe()),
974975
)
975976
rtol = atol = _get_tol_from_less_precise(check_less_precise)
976977

@@ -1263,7 +1264,7 @@ def assert_frame_equal(
12631264
"is deprecated and will be removed in a future version. "
12641265
"You can stop passing 'check_less_precise' to silence this warning.",
12651266
FutureWarning,
1266-
stacklevel=find_stack_level(),
1267+
stacklevel=find_stack_level(inspect.currentframe()),
12671268
)
12681269
rtol = atol = _get_tol_from_less_precise(check_less_precise)
12691270

pandas/core/accessor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from __future__ import annotations
88

9+
import inspect
910
import warnings
1011

1112
from pandas.util._decorators import doc
@@ -268,7 +269,7 @@ def decorator(accessor):
268269
f"{repr(name)} for type {repr(cls)} is overriding a preexisting "
269270
f"attribute with the same name.",
270271
UserWarning,
271-
stacklevel=find_stack_level(),
272+
stacklevel=find_stack_level(inspect.currentframe()),
272273
)
273274
setattr(cls, name, CachedAccessor(name, accessor))
274275
cls._accessors.add(name)

pandas/core/algorithms.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,9 @@ def resolve_na_sentinel(
836836
"Specify `use_na_sentinel=True` to use the sentinel value -1, and "
837837
"`use_na_sentinel=False` to encode NaN values."
838838
)
839-
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
839+
warnings.warn(
840+
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
841+
)
840842
result = na_sentinel
841843
return result
842844

@@ -1658,7 +1660,7 @@ def diff(arr, n: int, axis: int = 0):
16581660
"dtype lost in 'diff()'. In the future this will raise a "
16591661
"TypeError. Convert to a suitable dtype prior to calling 'diff'.",
16601662
FutureWarning,
1661-
stacklevel=find_stack_level(),
1663+
stacklevel=find_stack_level(inspect.currentframe()),
16621664
)
16631665
arr = np.asarray(arr)
16641666
dtype = arr.dtype

pandas/core/apply.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def transform_dict_like(self, func):
291291
f"raised, this will raise in a future version of pandas. "
292292
f"Drop these columns/ops to avoid this warning.",
293293
FutureWarning,
294-
stacklevel=find_stack_level(),
294+
stacklevel=find_stack_level(inspect.currentframe()),
295295
)
296296
return concat(results, axis=1)
297297

@@ -423,7 +423,7 @@ def agg_list_like(self) -> DataFrame | Series:
423423
warnings.warn(
424424
depr_nuisance_columns_msg.format(failed_names),
425425
FutureWarning,
426-
stacklevel=find_stack_level(),
426+
stacklevel=find_stack_level(inspect.currentframe()),
427427
)
428428

429429
try:

pandas/core/arraylike.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from __future__ import annotations
88

9+
import inspect
910
import operator
1011
from typing import Any
1112
import warnings
@@ -220,7 +221,7 @@ def _maybe_fallback(ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any):
220221
"or align manually (eg 'df1, df2 = df1.align(df2)') before passing to "
221222
"the ufunc to obtain the future behaviour and silence this warning.",
222223
FutureWarning,
223-
stacklevel=find_stack_level(),
224+
stacklevel=find_stack_level(inspect.currentframe()),
224225
)
225226

226227
# keep the first dataframe of the inputs, other DataFrame/Series is
@@ -348,7 +349,9 @@ def _reconstruct(result):
348349
"to an array with '.to_numpy()' first."
349350
)
350351
warnings.warn(
351-
msg.format(ufunc), FutureWarning, stacklevel=find_stack_level()
352+
msg.format(ufunc),
353+
FutureWarning,
354+
stacklevel=find_stack_level(inspect.currentframe()),
352355
)
353356
return result
354357
raise NotImplementedError

pandas/core/arrays/arrow/_arrow_utils.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import inspect
34
import json
45
import warnings
56

@@ -22,7 +23,9 @@ def fallback_performancewarning(version: str | None = None) -> None:
2223
msg = "Falling back on a non-pyarrow code path which may decrease performance."
2324
if version is not None:
2425
msg += f" Upgrade to pyarrow >={version} to possibly suppress this warning."
25-
warnings.warn(msg, PerformanceWarning, stacklevel=find_stack_level())
26+
warnings.warn(
27+
msg, PerformanceWarning, stacklevel=find_stack_level(inspect.currentframe())
28+
)
2629

2730

2831
def pyarrow_array_to_numpy_and_mask(
@@ -133,7 +136,7 @@ def closed(self) -> IntervalInclusiveType:
133136
warnings.warn(
134137
"Attribute `closed` is deprecated in favor of `inclusive`.",
135138
FutureWarning,
136-
stacklevel=find_stack_level(),
139+
stacklevel=find_stack_level(inspect.currentframe()),
137140
)
138141
return self._inclusive
139142

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def __init_subclass__(cls, **kwargs) -> None:
476476
f"instead. Add this argument to `{name}.factorize` to be compatible "
477477
f"with future versions of pandas and silence this warning.",
478478
DeprecationWarning,
479-
stacklevel=find_stack_level(),
479+
stacklevel=find_stack_level(inspect.currentframe()),
480480
)
481481

482482
def to_numpy(

pandas/core/arrays/categorical.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from csv import QUOTE_NONNUMERIC
44
from functools import partial
5+
import inspect
56
import operator
67
from shutil import get_terminal_size
78
from typing import (
@@ -394,7 +395,7 @@ def __init__(
394395
"Allowing scalars in the Categorical constructor is deprecated "
395396
"and will raise in a future version. Use `[value]` instead",
396397
FutureWarning,
397-
stacklevel=find_stack_level(),
398+
stacklevel=find_stack_level(inspect.currentframe()),
398399
)
399400
values = [values]
400401

@@ -749,7 +750,7 @@ def categories(self, categories) -> None:
749750
"Setting categories in-place is deprecated and will raise in a "
750751
"future version. Use rename_categories instead.",
751752
FutureWarning,
752-
stacklevel=find_stack_level(),
753+
stacklevel=find_stack_level(inspect.currentframe()),
753754
)
754755

755756
self._set_categories(categories)
@@ -873,7 +874,7 @@ def set_ordered(
873874
"a future version. setting ordered-ness on categories will always "
874875
"return a new Categorical object.",
875876
FutureWarning,
876-
stacklevel=find_stack_level(),
877+
stacklevel=find_stack_level(inspect.currentframe()),
877878
)
878879
else:
879880
inplace = False
@@ -1125,7 +1126,7 @@ def rename_categories(
11251126
"a future version. Removing unused categories will always "
11261127
"return a new Categorical object.",
11271128
FutureWarning,
1128-
stacklevel=find_stack_level(),
1129+
stacklevel=find_stack_level(inspect.currentframe()),
11291130
)
11301131
else:
11311132
inplace = False
@@ -1189,7 +1190,7 @@ def reorder_categories(self, new_categories, ordered=None, inplace=no_default):
11891190
"a future version. Reordering categories will always "
11901191
"return a new Categorical object.",
11911192
FutureWarning,
1192-
stacklevel=find_stack_level(),
1193+
stacklevel=find_stack_level(inspect.currentframe()),
11931194
)
11941195
else:
11951196
inplace = False
@@ -1273,7 +1274,7 @@ def add_categories(
12731274
"a future version. Removing unused categories will always "
12741275
"return a new Categorical object.",
12751276
FutureWarning,
1276-
stacklevel=find_stack_level(),
1277+
stacklevel=find_stack_level(inspect.currentframe()),
12771278
)
12781279
else:
12791280
inplace = False
@@ -1349,7 +1350,7 @@ def remove_categories(self, removals, inplace=no_default):
13491350
"a future version. Removing unused categories will always "
13501351
"return a new Categorical object.",
13511352
FutureWarning,
1352-
stacklevel=find_stack_level(),
1353+
stacklevel=find_stack_level(inspect.currentframe()),
13531354
)
13541355
else:
13551356
inplace = False
@@ -1437,7 +1438,7 @@ def remove_unused_categories(
14371438
"remove_unused_categories is deprecated and "
14381439
"will be removed in a future version.",
14391440
FutureWarning,
1440-
stacklevel=find_stack_level(),
1441+
stacklevel=find_stack_level(inspect.currentframe()),
14411442
)
14421443
else:
14431444
inplace = False
@@ -2046,7 +2047,7 @@ def to_dense(self) -> np.ndarray:
20462047
"Categorical.to_dense is deprecated and will be removed in "
20472048
"a future version. Use np.asarray(cat) instead.",
20482049
FutureWarning,
2049-
stacklevel=find_stack_level(),
2050+
stacklevel=find_stack_level(inspect.currentframe()),
20502051
)
20512052
return np.asarray(self)
20522053

@@ -2063,7 +2064,7 @@ def _codes(self, value: np.ndarray):
20632064
"Setting the codes on a Categorical is deprecated and will raise in "
20642065
"a future version. Create a new Categorical object instead",
20652066
FutureWarning,
2066-
stacklevel=find_stack_level(),
2067+
stacklevel=find_stack_level(inspect.currentframe()),
20672068
) # GH#40606
20682069
NDArrayBacked.__init__(self, value, self.dtype)
20692070

@@ -2088,7 +2089,7 @@ def take_nd(
20882089
warn(
20892090
"Categorical.take_nd is deprecated, use Categorical.take instead",
20902091
FutureWarning,
2091-
stacklevel=find_stack_level(),
2092+
stacklevel=find_stack_level(inspect.currentframe()),
20922093
)
20932094
return self.take(indexer, allow_fill=allow_fill, fill_value=fill_value)
20942095

@@ -2381,7 +2382,7 @@ def mode(self, dropna: bool = True) -> Categorical:
23812382
"Categorical.mode is deprecated and will be removed in a future version. "
23822383
"Use Series.mode instead.",
23832384
FutureWarning,
2384-
stacklevel=find_stack_level(),
2385+
stacklevel=find_stack_level(inspect.currentframe()),
23852386
)
23862387
return self._mode(dropna=dropna)
23872388

@@ -2524,7 +2525,7 @@ def is_dtype_equal(self, other) -> bool:
25242525
"Categorical.is_dtype_equal is deprecated and will be removed "
25252526
"in a future version",
25262527
FutureWarning,
2527-
stacklevel=find_stack_level(),
2528+
stacklevel=find_stack_level(inspect.currentframe()),
25282529
)
25292530
try:
25302531
return self._categories_match_up_to_permutation(other)
@@ -2648,7 +2649,7 @@ def replace(self, to_replace, value, inplace: bool = False) -> Categorical | Non
26482649
"Categorical.replace is deprecated and will be removed in a future "
26492650
"version. Use Series.replace directly instead.",
26502651
FutureWarning,
2651-
stacklevel=find_stack_level(),
2652+
stacklevel=find_stack_level(inspect.currentframe()),
26522653
)
26532654
return self._replace(to_replace=to_replace, value=value, inplace=inplace)
26542655

pandas/core/arrays/datetimelike.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
datetime,
55
timedelta,
66
)
7+
import inspect
78
import operator
89
from typing import (
910
TYPE_CHECKING,
@@ -469,7 +470,7 @@ def astype(self, dtype, copy: bool = True):
469470
"exactly the specified dtype instead of uint64, and will "
470471
"raise if that conversion overflows.",
471472
FutureWarning,
472-
stacklevel=find_stack_level(),
473+
stacklevel=find_stack_level(inspect.currentframe()),
473474
)
474475
elif (self.asi8 < 0).any():
475476
# GH#45034
@@ -479,7 +480,7 @@ def astype(self, dtype, copy: bool = True):
479480
"raise if the conversion overflows, as it did in this "
480481
"case with negative int64 values.",
481482
FutureWarning,
482-
stacklevel=find_stack_level(),
483+
stacklevel=find_stack_level(inspect.currentframe()),
483484
)
484485
elif dtype != np.int64:
485486
# GH#45034
@@ -489,7 +490,7 @@ def astype(self, dtype, copy: bool = True):
489490
"exactly the specified dtype instead of int64, and will "
490491
"raise if that conversion overflows.",
491492
FutureWarning,
492-
stacklevel=find_stack_level(),
493+
stacklevel=find_stack_level(inspect.currentframe()),
493494
)
494495

495496
if copy:
@@ -628,7 +629,7 @@ def _validate_shift_value(self, fill_value):
628629
FutureWarning,
629630
# There is no way to hard-code the level since this might be
630631
# reached directly or called from the Index or Block method
631-
stacklevel=find_stack_level(),
632+
stacklevel=find_stack_level(inspect.currentframe()),
632633
)
633634
fill_value = new_fill
634635

0 commit comments

Comments
 (0)