Skip to content

PERF cache find_stack_level #48023

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 2 commits into from
Aug 11, 2022
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
4 changes: 3 additions & 1 deletion pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import numbers
from operator import (
le,
Expand Down Expand Up @@ -45,6 +46,7 @@ cnp.import_array()
import warnings

from pandas._libs import lib

from pandas._libs cimport util
from pandas._libs.hashtable cimport Int64Vector
from pandas._libs.tslibs.timedeltas cimport _Timedelta
Expand Down Expand Up @@ -394,7 +396,7 @@ cdef class Interval(IntervalMixin):
warnings.warn(
"Attribute `closed` is deprecated in favor of `inclusive`.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return self.inclusive

Expand Down
11 changes: 6 additions & 5 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
from typing import (
Literal,
cast,
Expand Down Expand Up @@ -112,7 +113,7 @@ def assert_almost_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

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

Expand Down Expand Up @@ -815,7 +816,7 @@ def assert_extension_array_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -970,7 +971,7 @@ def assert_series_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -1263,7 +1264,7 @@ def assert_frame_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import annotations

import inspect
import warnings

from pandas.util._decorators import doc
Expand Down Expand Up @@ -268,7 +269,7 @@ def decorator(accessor):
f"{repr(name)} for type {repr(cls)} is overriding a preexisting "
f"attribute with the same name.",
UserWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
setattr(cls, name, CachedAccessor(name, accessor))
cls._accessors.add(name)
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,9 @@ def resolve_na_sentinel(
"Specify `use_na_sentinel=True` to use the sentinel value -1, and "
"`use_na_sentinel=False` to encode NaN values."
)
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
warnings.warn(
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
)
result = na_sentinel
return result

Expand Down Expand Up @@ -1658,7 +1660,7 @@ def diff(arr, n: int, axis: int = 0):
"dtype lost in 'diff()'. In the future this will raise a "
"TypeError. Convert to a suitable dtype prior to calling 'diff'.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
arr = np.asarray(arr)
dtype = arr.dtype
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def transform_dict_like(self, func):
f"raised, this will raise in a future version of pandas. "
f"Drop these columns/ops to avoid this warning.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return concat(results, axis=1)

Expand Down Expand Up @@ -423,7 +423,7 @@ def agg_list_like(self) -> DataFrame | Series:
warnings.warn(
depr_nuisance_columns_msg.format(failed_names),
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)

try:
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arraylike.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import annotations

import inspect
import operator
from typing import Any
import warnings
Expand Down Expand Up @@ -220,7 +221,7 @@ def _maybe_fallback(ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any):
"or align manually (eg 'df1, df2 = df1.align(df2)') before passing to "
"the ufunc to obtain the future behaviour and silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)

# keep the first dataframe of the inputs, other DataFrame/Series is
Expand Down Expand Up @@ -348,7 +349,9 @@ def _reconstruct(result):
"to an array with '.to_numpy()' first."
)
warnings.warn(
msg.format(ufunc), FutureWarning, stacklevel=find_stack_level()
msg.format(ufunc),
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
)
return result
raise NotImplementedError
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/arrow/_arrow_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
import json
import warnings

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


def pyarrow_array_to_numpy_and_mask(
Expand Down Expand Up @@ -133,7 +136,7 @@ def closed(self) -> IntervalInclusiveType:
warnings.warn(
"Attribute `closed` is deprecated in favor of `inclusive`.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return self._inclusive

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def __init_subclass__(cls, **kwargs) -> None:
f"instead. Add this argument to `{name}.factorize` to be compatible "
f"with future versions of pandas and silence this warning.",
DeprecationWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)

def to_numpy(
Expand Down
29 changes: 15 additions & 14 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from csv import QUOTE_NONNUMERIC
from functools import partial
import inspect
import operator
from shutil import get_terminal_size
from typing import (
Expand Down Expand Up @@ -394,7 +395,7 @@ def __init__(
"Allowing scalars in the Categorical constructor is deprecated "
"and will raise in a future version. Use `[value]` instead",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
values = [values]

Expand Down Expand Up @@ -749,7 +750,7 @@ def categories(self, categories) -> None:
"Setting categories in-place is deprecated and will raise in a "
"future version. Use rename_categories instead.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)

self._set_categories(categories)
Expand Down Expand Up @@ -873,7 +874,7 @@ def set_ordered(
"a future version. setting ordered-ness on categories will always "
"return a new Categorical object.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
inplace = False
Expand Down Expand Up @@ -1125,7 +1126,7 @@ def rename_categories(
"a future version. Removing unused categories will always "
"return a new Categorical object.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
inplace = False
Expand Down Expand Up @@ -1189,7 +1190,7 @@ def reorder_categories(self, new_categories, ordered=None, inplace=no_default):
"a future version. Reordering categories will always "
"return a new Categorical object.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
inplace = False
Expand Down Expand Up @@ -1273,7 +1274,7 @@ def add_categories(
"a future version. Removing unused categories will always "
"return a new Categorical object.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
inplace = False
Expand Down Expand Up @@ -1349,7 +1350,7 @@ def remove_categories(self, removals, inplace=no_default):
"a future version. Removing unused categories will always "
"return a new Categorical object.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
inplace = False
Expand Down Expand Up @@ -1437,7 +1438,7 @@ def remove_unused_categories(
"remove_unused_categories is deprecated and "
"will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
inplace = False
Expand Down Expand Up @@ -2046,7 +2047,7 @@ def to_dense(self) -> np.ndarray:
"Categorical.to_dense is deprecated and will be removed in "
"a future version. Use np.asarray(cat) instead.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return np.asarray(self)

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

Expand All @@ -2088,7 +2089,7 @@ def take_nd(
warn(
"Categorical.take_nd is deprecated, use Categorical.take instead",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return self.take(indexer, allow_fill=allow_fill, fill_value=fill_value)

Expand Down Expand Up @@ -2381,7 +2382,7 @@ def mode(self, dropna: bool = True) -> Categorical:
"Categorical.mode is deprecated and will be removed in a future version. "
"Use Series.mode instead.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return self._mode(dropna=dropna)

Expand Down Expand Up @@ -2524,7 +2525,7 @@ def is_dtype_equal(self, other) -> bool:
"Categorical.is_dtype_equal is deprecated and will be removed "
"in a future version",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
try:
return self._categories_match_up_to_permutation(other)
Expand Down Expand Up @@ -2648,7 +2649,7 @@ def replace(self, to_replace, value, inplace: bool = False) -> Categorical | Non
"Categorical.replace is deprecated and will be removed in a future "
"version. Use Series.replace directly instead.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
return self._replace(to_replace=to_replace, value=value, inplace=inplace)

Expand Down
9 changes: 5 additions & 4 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
datetime,
timedelta,
)
import inspect
import operator
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -469,7 +470,7 @@ def astype(self, dtype, copy: bool = True):
"exactly the specified dtype instead of uint64, and will "
"raise if that conversion overflows.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
elif (self.asi8 < 0).any():
# GH#45034
Expand All @@ -479,7 +480,7 @@ def astype(self, dtype, copy: bool = True):
"raise if the conversion overflows, as it did in this "
"case with negative int64 values.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
elif dtype != np.int64:
# GH#45034
Expand All @@ -489,7 +490,7 @@ def astype(self, dtype, copy: bool = True):
"exactly the specified dtype instead of int64, and will "
"raise if that conversion overflows.",
FutureWarning,
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)

if copy:
Expand Down Expand Up @@ -628,7 +629,7 @@ def _validate_shift_value(self, fill_value):
FutureWarning,
# There is no way to hard-code the level since this might be
# reached directly or called from the Index or Block method
stacklevel=find_stack_level(),
stacklevel=find_stack_level(inspect.currentframe()),
)
fill_value = new_fill

Expand Down
Loading