Skip to content

Commit 78aa87b

Browse files
authored
ENH: Use stacklevel in warnings (#44439)
1 parent 143f582 commit 78aa87b

File tree

12 files changed

+28
-34
lines changed

12 files changed

+28
-34
lines changed

pandas/core/algorithms.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
npt,
3636
)
3737
from pandas.util._decorators import doc
38+
from pandas.util._exceptions import find_stack_level
3839

3940
from pandas.core.dtypes.cast import (
4041
construct_1d_object_array_from_listlike,
@@ -1550,7 +1551,7 @@ def searchsorted(
15501551
_diff_special = {"float64", "float32", "int64", "int32", "int16", "int8"}
15511552

15521553

1553-
def diff(arr, n: int, axis: int = 0, stacklevel: int = 3):
1554+
def diff(arr, n: int, axis: int = 0):
15541555
"""
15551556
difference of n between self,
15561557
analogous to s-s.shift(n)
@@ -1596,7 +1597,7 @@ def diff(arr, n: int, axis: int = 0, stacklevel: int = 3):
15961597
"dtype lost in 'diff()'. In the future this will raise a "
15971598
"TypeError. Convert to a suitable dtype prior to calling 'diff'.",
15981599
FutureWarning,
1599-
stacklevel=stacklevel,
1600+
stacklevel=find_stack_level(),
16001601
)
16011602
arr = np.asarray(arr)
16021603
dtype = arr.dtype

pandas/core/arraylike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ def reconstruct(result):
337337
"Consider explicitly converting the DataFrame "
338338
"to an array with '.to_numpy()' first."
339339
)
340-
warnings.warn(msg.format(ufunc), FutureWarning, stacklevel=4)
340+
warnings.warn(
341+
msg.format(ufunc), FutureWarning, stacklevel=find_stack_level()
342+
)
341343
return result
342344
raise NotImplementedError
343345
return result

pandas/core/arrays/datetimelike.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,12 @@ def astype(self, dtype, copy: bool = True):
416416
elif is_integer_dtype(dtype):
417417
# we deliberately ignore int32 vs. int64 here.
418418
# See https://github.com/pandas-dev/pandas/issues/24381 for more.
419-
level = find_stack_level()
420419
warnings.warn(
421420
f"casting {self.dtype} values to int64 with .astype(...) is "
422421
"deprecated and will raise in a future version. "
423422
"Use .view(...) instead.",
424423
FutureWarning,
425-
stacklevel=level,
424+
stacklevel=find_stack_level(),
426425
)
427426

428427
values = self.asi8

pandas/core/dtypes/cast.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -969,13 +969,12 @@ def astype_dt64_to_dt64tz(
969969
# this should be the only copy
970970
values = values.copy()
971971

972-
level = find_stack_level()
973972
warnings.warn(
974973
"Using .astype to convert from timezone-naive dtype to "
975974
"timezone-aware dtype is deprecated and will raise in a "
976975
"future version. Use ser.dt.tz_localize instead.",
977976
FutureWarning,
978-
stacklevel=level,
977+
stacklevel=find_stack_level(),
979978
)
980979

981980
# GH#33401 this doesn't match DatetimeArray.astype, which
@@ -986,13 +985,12 @@ def astype_dt64_to_dt64tz(
986985
# DatetimeArray/DatetimeIndex.astype behavior
987986
if values.tz is None and aware:
988987
dtype = cast(DatetimeTZDtype, dtype)
989-
level = find_stack_level()
990988
warnings.warn(
991989
"Using .astype to convert from timezone-naive dtype to "
992990
"timezone-aware dtype is deprecated and will raise in a "
993991
"future version. Use obj.tz_localize instead.",
994992
FutureWarning,
995-
stacklevel=level,
993+
stacklevel=find_stack_level(),
996994
)
997995

998996
return values.tz_localize(dtype.tz)
@@ -1006,14 +1004,13 @@ def astype_dt64_to_dt64tz(
10061004
return result
10071005

10081006
elif values.tz is not None:
1009-
level = find_stack_level()
10101007
warnings.warn(
10111008
"Using .astype to convert from timezone-aware dtype to "
10121009
"timezone-naive dtype is deprecated and will raise in a "
10131010
"future version. Use obj.tz_localize(None) or "
10141011
"obj.tz_convert('UTC').tz_localize(None) instead",
10151012
FutureWarning,
1016-
stacklevel=level,
1013+
stacklevel=find_stack_level(),
10171014
)
10181015

10191016
result = values.tz_convert("UTC").tz_localize(None)

pandas/core/generic.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,10 @@ def _data(self):
487487
@property
488488
def _AXIS_NUMBERS(self) -> dict[str, int]:
489489
""".. deprecated:: 1.1.0"""
490-
level = self.ndim + 1
491490
warnings.warn(
492-
"_AXIS_NUMBERS has been deprecated.", FutureWarning, stacklevel=level
491+
"_AXIS_NUMBERS has been deprecated.",
492+
FutureWarning,
493+
stacklevel=find_stack_level(),
493494
)
494495
return {"index": 0}
495496

pandas/core/groupby/grouper.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from pandas.errors import InvalidIndexError
2323
from pandas.util._decorators import cache_readonly
24+
from pandas.util._exceptions import find_stack_level
2425

2526
from pandas.core.dtypes.cast import sanitize_to_nanoseconds
2627
from pandas.core.dtypes.common import (
@@ -964,8 +965,6 @@ def _check_deprecated_resample_kwargs(kwargs, origin):
964965
From where this function is being called; either Grouper or TimeGrouper. Used
965966
to determine an approximate stacklevel.
966967
"""
967-
from pandas.core.resample import TimeGrouper
968-
969968
# Deprecation warning of `base` and `loffset` since v1.1.0:
970969
# we are raising the warning here to be able to set the `stacklevel`
971970
# properly since we need to raise the `base` and `loffset` deprecation
@@ -975,11 +974,6 @@ def _check_deprecated_resample_kwargs(kwargs, origin):
975974
# core/groupby/grouper.py::Grouper
976975
# raising these warnings from TimeGrouper directly would fail the test:
977976
# tests/resample/test_deprecated.py::test_deprecating_on_loffset_and_base
978-
# hacky way to set the stacklevel: if cls is TimeGrouper it means
979-
# that the call comes from a pandas internal call of resample,
980-
# otherwise it comes from pd.Grouper
981-
stacklevel = (5 if origin is TimeGrouper else 2) + 1
982-
# the + 1 is for this helper function, check_deprecated_resample_kwargs
983977

984978
if kwargs.get("base", None) is not None:
985979
warnings.warn(
@@ -989,7 +983,7 @@ def _check_deprecated_resample_kwargs(kwargs, origin):
989983
"\nbecomes:\n"
990984
'\n>>> df.resample(freq="3s", offset="2s")\n',
991985
FutureWarning,
992-
stacklevel=stacklevel,
986+
stacklevel=find_stack_level(),
993987
)
994988
if kwargs.get("loffset", None) is not None:
995989
warnings.warn(
@@ -1000,5 +994,5 @@ def _check_deprecated_resample_kwargs(kwargs, origin):
1000994
'\n>>> df = df.resample(freq="3s").mean()'
1001995
'\n>>> df.index = df.index.to_timestamp() + to_offset("8H")\n',
1002996
FutureWarning,
1003-
stacklevel=stacklevel,
997+
stacklevel=find_stack_level(),
1004998
)

pandas/core/internals/array_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def diff(self: T, n: int, axis: int) -> T:
365365
# with axis=0 is equivalent
366366
assert n == 0
367367
axis = 0
368-
return self.apply(algos.diff, n=n, axis=axis, stacklevel=5)
368+
return self.apply(algos.diff, n=n, axis=axis)
369369

370370
def interpolate(self: T, **kwargs) -> T:
371371
return self.apply_with_block("interpolate", swap_axis=False, **kwargs)

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ def take_nd(
11281128

11291129
def diff(self, n: int, axis: int = 1) -> list[Block]:
11301130
"""return block for the diff of the values"""
1131-
new_values = algos.diff(self.values, n, axis=axis, stacklevel=7)
1131+
new_values = algos.diff(self.values, n, axis=axis)
11321132
return [self.make_block(values=new_values)]
11331133

11341134
def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Block]:

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def _get_values_tuple(self, key):
10121012
# mpl hackaround
10131013
if com.any_none(*key):
10141014
result = self._get_values(key)
1015-
deprecate_ndim_indexing(result, stacklevel=5)
1015+
deprecate_ndim_indexing(result, stacklevel=find_stack_level())
10161016
return result
10171017

10181018
if not isinstance(self.index, MultiIndex):

pandas/core/strings/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ def replace(
14271427
" In addition, single character regular expressions will "
14281428
"*not* be treated as literal strings when regex=True."
14291429
)
1430-
warnings.warn(msg, FutureWarning, stacklevel=3)
1430+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
14311431

14321432
# Check whether repl is valid (GH 13438, GH 15055)
14331433
if not (isinstance(repl, str) or callable(repl)):

pandas/io/excel/_base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,10 @@ def parse(
519519
if convert_float is None:
520520
convert_float = True
521521
else:
522-
stacklevel = find_stack_level()
523522
warnings.warn(
524523
"convert_float is deprecated and will be removed in a future version.",
525524
FutureWarning,
526-
stacklevel=stacklevel,
525+
stacklevel=find_stack_level(),
527526
)
528527

529528
validate_header_arg(header)

pandas/io/formats/style.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from pandas.compat._optional import import_optional_dependency
3030
from pandas.util._decorators import doc
31+
from pandas.util._exceptions import find_stack_level
3132

3233
import pandas as pd
3334
from pandas import (
@@ -310,7 +311,7 @@ def render(
310311
warnings.warn(
311312
"this method is deprecated in favour of `Styler.to_html()`",
312313
FutureWarning,
313-
stacklevel=2,
314+
stacklevel=find_stack_level(),
314315
)
315316
if sparse_index is None:
316317
sparse_index = get_option("styler.sparse.index")
@@ -1675,7 +1676,7 @@ def where(
16751676
warnings.warn(
16761677
"this method is deprecated in favour of `Styler.applymap()`",
16771678
FutureWarning,
1678-
stacklevel=2,
1679+
stacklevel=find_stack_level(),
16791680
)
16801681

16811682
if other is None:
@@ -1707,7 +1708,7 @@ def set_precision(self, precision: int) -> StylerRenderer:
17071708
warnings.warn(
17081709
"this method is deprecated in favour of `Styler.format(precision=..)`",
17091710
FutureWarning,
1710-
stacklevel=2,
1711+
stacklevel=find_stack_level(),
17111712
)
17121713
self.precision = precision
17131714
return self.format(precision=precision, na_rep=self.na_rep)
@@ -2217,7 +2218,7 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer:
22172218
warnings.warn(
22182219
"this method is deprecated in favour of `Styler.format(na_rep=..)`",
22192220
FutureWarning,
2220-
stacklevel=2,
2221+
stacklevel=find_stack_level(),
22212222
)
22222223
self.na_rep = na_rep
22232224
return self.format(na_rep=na_rep, precision=self.precision)
@@ -2271,7 +2272,7 @@ def hide_index(
22712272
warnings.warn(
22722273
"this method is deprecated in favour of `Styler.hide(axis='index')`",
22732274
FutureWarning,
2274-
stacklevel=2,
2275+
stacklevel=find_stack_level(),
22752276
)
22762277
return self.hide(axis=0, level=level, subset=subset, names=names)
22772278

@@ -2324,7 +2325,7 @@ def hide_columns(
23242325
warnings.warn(
23252326
"this method is deprecated in favour of `Styler.hide(axis='columns')`",
23262327
FutureWarning,
2327-
stacklevel=2,
2328+
stacklevel=find_stack_level(),
23282329
)
23292330
return self.hide(axis=1, level=level, subset=subset, names=names)
23302331

0 commit comments

Comments
 (0)