Skip to content

Commit 077ff18

Browse files
MarcoGorellinoatamir
authored andcommitted
REVERT caching in find_stack_level (pandas-dev#49053)
Revert "PERF cache find_stack_level (pandas-dev#48023)" This reverts commit 2f8d0a3. Co-authored-by: MarcoGorelli <>
1 parent 350654d commit 077ff18

Some content is hidden

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

98 files changed

+332
-479
lines changed

doc/source/development/contributing_codebase.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Otherwise, you need to do it manually:
139139
warnings.warn(
140140
'Use new_func instead.',
141141
FutureWarning,
142-
stacklevel=find_stack_level(inspect.currentframe()),
142+
stacklevel=find_stack_level(),
143143
)
144144
new_func()
145145

pandas/_config/config.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
ContextDecorator,
5555
contextmanager,
5656
)
57-
import inspect
5857
import re
5958
from typing import (
6059
Any,
@@ -662,7 +661,7 @@ def _warn_if_deprecated(key: str) -> bool:
662661
warnings.warn(
663662
d.msg,
664663
FutureWarning,
665-
stacklevel=find_stack_level(inspect.currentframe()),
664+
stacklevel=find_stack_level(),
666665
)
667666
else:
668667
msg = f"'{key}' is deprecated"
@@ -673,9 +672,7 @@ def _warn_if_deprecated(key: str) -> bool:
673672
else:
674673
msg += ", please refrain from using it."
675674

676-
warnings.warn(
677-
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
678-
)
675+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
679676
return True
680677
return False
681678

pandas/_libs/parsers.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ cdef class TextReader:
972972
"Defining usecols with out of bounds indices is deprecated "
973973
"and will raise a ParserError in a future version.",
974974
FutureWarning,
975-
stacklevel=find_stack_level(inspect.currentframe()),
975+
stacklevel=find_stack_level(),
976976
)
977977

978978
results = {}
@@ -1023,7 +1023,7 @@ cdef class TextReader:
10231023
warnings.warn((f"Both a converter and dtype were specified "
10241024
f"for column {name} - only the converter will "
10251025
f"be used."), ParserWarning,
1026-
stacklevel=find_stack_level(inspect.currentframe()))
1026+
stacklevel=find_stack_level())
10271027
results[i] = _apply_converter(conv, self.parser, i, start, end)
10281028
continue
10291029

pandas/_libs/tslib.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
cimport cython
@@ -847,7 +846,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
847846
"deprecated. In a future version, this will match Timestamp('now') "
848847
"and Timestamp.now()",
849848
FutureWarning,
850-
stacklevel=find_stack_level(inspect.currentframe()),
849+
stacklevel=find_stack_level(),
851850
)
852851

853852
return True

pandas/_libs/tslibs/conversion.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
import numpy as np
@@ -286,7 +285,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
286285
"Conversion of non-round float with unit={unit} is ambiguous "
287286
"and will raise in a future version.",
288287
FutureWarning,
289-
stacklevel=find_stack_level(inspect.currentframe()),
288+
stacklevel=find_stack_level(),
290289
)
291290

292291
ts = cast_from_unit(ts, unit)

pandas/_libs/tslibs/nattype.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
from pandas.util._exceptions import find_stack_level
@@ -134,7 +133,7 @@ cdef class _NaT(datetime):
134133
"order to match the standard library behavior. "
135134
"In a future version these will be considered non-comparable.",
136135
FutureWarning,
137-
stacklevel=find_stack_level(inspect.currentframe()),
136+
stacklevel=find_stack_level(),
138137
)
139138
return False
140139

@@ -378,7 +377,7 @@ class NaTType(_NaT):
378377
warnings.warn(
379378
"NaT.freq is deprecated and will be removed in a future version.",
380379
FutureWarning,
381-
stacklevel=find_stack_level(inspect.currentframe()),
380+
stacklevel=find_stack_level(),
382381
)
383382
return None
384383

pandas/_libs/tslibs/offsets.pyx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import re
32
import time
43
import warnings
@@ -501,7 +500,7 @@ cdef class BaseOffset:
501500
"DateOffset.__call__ is deprecated and will be removed in a future "
502501
"version. Use `offset + other` instead.",
503502
FutureWarning,
504-
stacklevel=find_stack_level(inspect.currentframe()),
503+
stacklevel=find_stack_level(),
505504
)
506505
return self._apply(other)
507506

@@ -511,7 +510,7 @@ cdef class BaseOffset:
511510
f"{type(self).__name__}.apply is deprecated and will be removed "
512511
"in a future version. Use `offset + other` instead",
513512
FutureWarning,
514-
stacklevel=find_stack_level(inspect.currentframe()),
513+
stacklevel=find_stack_level(),
515514
)
516515
return self._apply(other)
517516

@@ -825,15 +824,15 @@ cdef class BaseOffset:
825824
warnings.warn(
826825
"onOffset is a deprecated, use is_on_offset instead.",
827826
FutureWarning,
828-
stacklevel=find_stack_level(inspect.currentframe()),
827+
stacklevel=find_stack_level(),
829828
)
830829
return self.is_on_offset(dt)
831830

832831
def isAnchored(self) -> bool:
833832
warnings.warn(
834833
"isAnchored is a deprecated, use is_anchored instead.",
835834
FutureWarning,
836-
stacklevel=find_stack_level(inspect.currentframe()),
835+
stacklevel=find_stack_level(),
837836
)
838837
return self.is_anchored()
839838

pandas/_libs/tslibs/parsing.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Parsing functions for datetime and datetime-like strings.
33
"""
4-
import inspect
54
import re
65
import time
76
import warnings
@@ -218,15 +217,15 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
218217
format='MM/DD/YYYY',
219218
dayfirst='True',
220219
),
221-
stacklevel=find_stack_level(inspect.currentframe()),
220+
stacklevel=find_stack_level(),
222221
)
223222
elif not dayfirst and swapped_day_and_month:
224223
warnings.warn(
225224
PARSING_WARNING_MSG.format(
226225
format='DD/MM/YYYY',
227226
dayfirst='False (the default)',
228227
),
229-
stacklevel=find_stack_level(inspect.currentframe()),
228+
stacklevel=find_stack_level(),
230229
)
231230
# In Python <= 3.6.0 there is no range checking for invalid dates
232231
# in C api, thus we call faster C version for 3.6.1 or newer

pandas/_libs/tslibs/period.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
from pandas.util._exceptions import find_stack_level
@@ -1830,7 +1829,7 @@ cdef class _Period(PeriodMixin):
18301829
"be removed in a future version. Use "
18311830
"`per.to_timestamp(...).tz_localize(tz)` instead.",
18321831
FutureWarning,
1833-
stacklevel=find_stack_level(inspect.currentframe()),
1832+
stacklevel=find_stack_level(),
18341833
)
18351834

18361835
how = validate_end_alias(how)

pandas/_libs/tslibs/timedeltas.pyx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import collections
2-
import inspect
32
import warnings
43

54
from pandas.util._exceptions import find_stack_level
@@ -687,7 +686,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
687686
"Units 'M', 'Y' and 'y' do not represent unambiguous "
688687
"timedelta values and will be removed in a future version.",
689688
FutureWarning,
690-
stacklevel=find_stack_level(inspect.currentframe()),
689+
stacklevel=find_stack_level(),
691690
)
692691

693692
if unit == 'M':
@@ -1066,7 +1065,7 @@ cdef class _Timedelta(timedelta):
10661065
warnings.warn(
10671066
"Timedelta.freq is deprecated and will be removed in a future version",
10681067
FutureWarning,
1069-
stacklevel=find_stack_level(inspect.currentframe()),
1068+
stacklevel=find_stack_level(),
10701069
)
10711070
return None
10721071

@@ -1082,7 +1081,7 @@ cdef class _Timedelta(timedelta):
10821081
warnings.warn(
10831082
"Timedelta.is_populated is deprecated and will be removed in a future version",
10841083
FutureWarning,
1085-
stacklevel=find_stack_level(inspect.currentframe()),
1084+
stacklevel=find_stack_level(),
10861085
)
10871086
return self._is_populated
10881087

@@ -1289,7 +1288,7 @@ cdef class _Timedelta(timedelta):
12891288
warnings.warn(
12901289
"Timedelta.delta is deprecated and will be removed in a future version.",
12911290
FutureWarning,
1292-
stacklevel=find_stack_level(inspect.currentframe()),
1291+
stacklevel=find_stack_level(),
12931292
)
12941293
return self.value
12951294

pandas/_libs/tslibs/timestamps.pyx

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ construction requirements, we need to do object instantiation in python
66
(see Timestamp class below). This will serve as a C extension type that
77
shadows the python class, where we do any heavy lifting.
88
"""
9-
import inspect
109
import warnings
1110

1211
cimport cython
@@ -251,7 +250,7 @@ cdef class _Timestamp(ABCTimestamp):
251250
warnings.warn(
252251
"Timestamp.freq is deprecated and will be removed in a future version.",
253252
FutureWarning,
254-
stacklevel=find_stack_level(inspect.currentframe()),
253+
stacklevel=find_stack_level(),
255254
)
256255
return self._freq
257256

@@ -371,7 +370,7 @@ cdef class _Timestamp(ABCTimestamp):
371370
"In a future version these will be considered non-comparable. "
372371
"Use 'ts == pd.Timestamp(date)' or 'ts.date() == date' instead.",
373372
FutureWarning,
374-
stacklevel=find_stack_level(inspect.currentframe()),
373+
stacklevel=find_stack_level(),
375374
)
376375
return NotImplemented
377376
else:
@@ -626,7 +625,7 @@ cdef class _Timestamp(ABCTimestamp):
626625
"version. When you have a freq, use "
627626
f"freq.{field}(timestamp) instead.",
628627
FutureWarning,
629-
stacklevel=find_stack_level(inspect.currentframe()),
628+
stacklevel=find_stack_level(),
630629
)
631630

632631
@property
@@ -1132,7 +1131,7 @@ cdef class _Timestamp(ABCTimestamp):
11321131
"""
11331132
if self.nanosecond != 0 and warn:
11341133
warnings.warn("Discarding nonzero nanoseconds in conversion.",
1135-
UserWarning, stacklevel=find_stack_level(inspect.currentframe()))
1134+
UserWarning, stacklevel=find_stack_level())
11361135

11371136
return datetime(self.year, self.month, self.day,
11381137
self.hour, self.minute, self.second,
@@ -1211,7 +1210,7 @@ cdef class _Timestamp(ABCTimestamp):
12111210
warnings.warn(
12121211
"Converting to Period representation will drop timezone information.",
12131212
UserWarning,
1214-
stacklevel=find_stack_level(inspect.currentframe()),
1213+
stacklevel=find_stack_level(),
12151214
)
12161215

12171216
if freq is None:
@@ -1220,7 +1219,7 @@ cdef class _Timestamp(ABCTimestamp):
12201219
"In a future version, calling 'Timestamp.to_period()' without "
12211220
"passing a 'freq' will raise an exception.",
12221221
FutureWarning,
1223-
stacklevel=find_stack_level(inspect.currentframe()),
1222+
stacklevel=find_stack_level(),
12241223
)
12251224

12261225
return Period(self, freq=freq)
@@ -1412,7 +1411,7 @@ class Timestamp(_Timestamp):
14121411
"Timestamp.utcfromtimestamp(ts).tz_localize(None). "
14131412
"To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')",
14141413
FutureWarning,
1415-
stacklevel=find_stack_level(inspect.currentframe()),
1414+
stacklevel=find_stack_level(),
14161415
)
14171416
return cls(datetime.utcfromtimestamp(ts))
14181417

@@ -1648,7 +1647,7 @@ class Timestamp(_Timestamp):
16481647
"as a wall time, not a UTC time. To interpret as a UTC time, "
16491648
"use `Timestamp(dt64).tz_localize('UTC').tz_convert(tz)`",
16501649
FutureWarning,
1651-
stacklevel=find_stack_level(inspect.currentframe()),
1650+
stacklevel=find_stack_level(),
16521651
)
16531652
# Once this deprecation is enforced, we can do
16541653
# return Timestamp(ts_input).tz_localize(tzobj)
@@ -1671,7 +1670,7 @@ class Timestamp(_Timestamp):
16711670
"The 'freq' argument in Timestamp is deprecated and will be "
16721671
"removed in a future version.",
16731672
FutureWarning,
1674-
stacklevel=find_stack_level(inspect.currentframe()),
1673+
stacklevel=find_stack_level(),
16751674
)
16761675
if not is_offset_object(freq):
16771676
freq = to_offset(freq)
@@ -2007,7 +2006,7 @@ timedelta}, default 'raise'
20072006
warnings.warn(
20082007
"Timestamp.freqstr is deprecated and will be removed in a future version.",
20092008
FutureWarning,
2010-
stacklevel=find_stack_level(inspect.currentframe()),
2009+
stacklevel=find_stack_level(),
20112010
)
20122011
return self._freqstr
20132012

pandas/_testing/asserters.py

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

3-
import inspect
43
from typing import (
54
Literal,
65
cast,
@@ -113,7 +112,7 @@ def assert_almost_equal(
113112
"is deprecated and will be removed in a future version. "
114113
"You can stop passing 'check_less_precise' to silence this warning.",
115114
FutureWarning,
116-
stacklevel=find_stack_level(inspect.currentframe()),
115+
stacklevel=find_stack_level(),
117116
)
118117
rtol = atol = _get_tol_from_less_precise(check_less_precise)
119118

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

@@ -830,7 +829,7 @@ def assert_extension_array_equal(
830829
"is deprecated and will be removed in a future version. "
831830
"You can stop passing 'check_less_precise' to silence this warning.",
832831
FutureWarning,
833-
stacklevel=find_stack_level(inspect.currentframe()),
832+
stacklevel=find_stack_level(),
834833
)
835834
rtol = atol = _get_tol_from_less_precise(check_less_precise)
836835

@@ -985,7 +984,7 @@ def assert_series_equal(
985984
"is deprecated and will be removed in a future version. "
986985
"You can stop passing 'check_less_precise' to silence this warning.",
987986
FutureWarning,
988-
stacklevel=find_stack_level(inspect.currentframe()),
987+
stacklevel=find_stack_level(),
989988
)
990989
rtol = atol = _get_tol_from_less_precise(check_less_precise)
991990

@@ -1278,7 +1277,7 @@ def assert_frame_equal(
12781277
"is deprecated and will be removed in a future version. "
12791278
"You can stop passing 'check_less_precise' to silence this warning.",
12801279
FutureWarning,
1281-
stacklevel=find_stack_level(inspect.currentframe()),
1280+
stacklevel=find_stack_level(),
12821281
)
12831282
rtol = atol = _get_tol_from_less_precise(check_less_precise)
12841283

pandas/compat/_optional.py

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

33
import importlib
4-
import inspect
54
import sys
65
import types
76
import warnings
@@ -165,7 +164,7 @@ def import_optional_dependency(
165164
warnings.warn(
166165
msg,
167166
UserWarning,
168-
stacklevel=find_stack_level(inspect.currentframe()),
167+
stacklevel=find_stack_level(),
169168
)
170169
return None
171170
elif errors == "raise":

0 commit comments

Comments
 (0)