Skip to content

Commit

Permalink
REVERT caching in find_stack_level (pandas-dev#49053)
Browse files Browse the repository at this point in the history
Revert "PERF cache find_stack_level (pandas-dev#48023)"

This reverts commit 2f8d0a3.

Co-authored-by: MarcoGorelli <>
(cherry picked from commit 0106c26)
  • Loading branch information
MarcoGorelli authored and MarcoGorelli committed Oct 14, 2022
1 parent b8d2f46 commit c677ae4
Show file tree
Hide file tree
Showing 98 changed files with 330 additions and 478 deletions.
2 changes: 1 addition & 1 deletion doc/source/development/contributing_codebase.rst
Expand Up @@ -134,7 +134,7 @@ Otherwise, you need to do it manually:
warnings.warn(
'Use new_func instead.',
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
new_func()
Expand Down
7 changes: 2 additions & 5 deletions pandas/_config/config.py
Expand Up @@ -54,7 +54,6 @@
ContextDecorator,
contextmanager,
)
import inspect
import re
from typing import (
Any,
Expand Down Expand Up @@ -662,7 +661,7 @@ def _warn_if_deprecated(key: str) -> bool:
warnings.warn(
d.msg,
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
else:
msg = f"'{key}' is deprecated"
Expand All @@ -673,9 +672,7 @@ def _warn_if_deprecated(key: str) -> bool:
else:
msg += ", please refrain from using it."

warnings.warn(
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
)
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
return True
return False

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/parsers.pyx
Expand Up @@ -961,7 +961,7 @@ cdef class TextReader:
"Defining usecols with out of bounds indices is deprecated "
"and will raise a ParserError in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

results = {}
Expand Down Expand Up @@ -1012,7 +1012,7 @@ cdef class TextReader:
warnings.warn((f"Both a converter and dtype were specified "
f"for column {name} - only the converter will "
f"be used."), ParserWarning,
stacklevel=find_stack_level(inspect.currentframe()))
stacklevel=find_stack_level())
results[i] = _apply_converter(conv, self.parser, i, start, end)
continue

Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslib.pyx
@@ -1,4 +1,3 @@
import inspect
import warnings

cimport cython
Expand Down Expand Up @@ -848,7 +847,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
"deprecated. In a future version, this will match Timestamp('now') "
"and Timestamp.now()",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

return True
Expand Down
4 changes: 1 addition & 3 deletions pandas/_libs/tslibs/conversion.pyx
@@ -1,5 +1,3 @@
import inspect

cimport cython

import warnings
Expand Down Expand Up @@ -291,7 +289,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
"Conversion of non-round float with unit={unit} is ambiguous "
"and will raise in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

ts = cast_from_unit(ts, unit)
Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/nattype.pyx
@@ -1,4 +1,3 @@
import inspect
import warnings

from pandas.util._exceptions import find_stack_level
Expand Down Expand Up @@ -138,7 +137,7 @@ cdef class _NaT(datetime):
"order to match the standard library behavior. "
"In a future version these will be considered non-comparable.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return False

Expand Down Expand Up @@ -382,7 +381,7 @@ class NaTType(_NaT):
warnings.warn(
"NaT.freq is deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return None

Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/tslibs/offsets.pyx
@@ -1,4 +1,3 @@
import inspect
import operator
import re
import time
Expand Down Expand Up @@ -502,7 +501,7 @@ cdef class BaseOffset:
"DateOffset.__call__ is deprecated and will be removed in a future "
"version. Use `offset + other` instead.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self._apply(other)

Expand All @@ -512,7 +511,7 @@ cdef class BaseOffset:
f"{type(self).__name__}.apply is deprecated and will be removed "
"in a future version. Use `offset + other` instead",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self._apply(other)

Expand Down Expand Up @@ -823,15 +822,15 @@ cdef class BaseOffset:
warnings.warn(
"onOffset is a deprecated, use is_on_offset instead.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self.is_on_offset(dt)

def isAnchored(self) -> bool:
warnings.warn(
"isAnchored is a deprecated, use is_anchored instead.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self.is_anchored()

Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/parsing.pyx
@@ -1,7 +1,6 @@
"""
Parsing functions for datetime and datetime-like strings.
"""
import inspect
import re
import time
import warnings
Expand Down Expand Up @@ -217,15 +216,15 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
format='MM/DD/YYYY',
dayfirst='True',
),
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
elif not dayfirst and swapped_day_and_month:
warnings.warn(
PARSING_WARNING_MSG.format(
format='DD/MM/YYYY',
dayfirst='False (the default)',
),
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
# In Python <= 3.6.0 there is no range checking for invalid dates
# in C api, thus we call faster C version for 3.6.1 or newer
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/period.pyx
@@ -1,4 +1,3 @@
import inspect
import warnings

from pandas.util._exceptions import find_stack_level
Expand Down Expand Up @@ -1830,7 +1829,7 @@ cdef class _Period(PeriodMixin):
"be removed in a future version. Use "
"`per.to_timestamp(...).tz_localize(tz)` instead.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

how = validate_end_alias(how)
Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/tslibs/timedeltas.pyx
@@ -1,5 +1,4 @@
import collections
import inspect
import warnings

from pandas.util._exceptions import find_stack_level
Expand Down Expand Up @@ -684,7 +683,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
"Units 'M', 'Y' and 'y' do not represent unambiguous "
"timedelta values and will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

if unit == 'M':
Expand Down Expand Up @@ -1062,7 +1061,7 @@ cdef class _Timedelta(timedelta):
warnings.warn(
"Timedelta.freq is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return None

Expand All @@ -1078,7 +1077,7 @@ cdef class _Timedelta(timedelta):
warnings.warn(
"Timedelta.is_populated is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self._is_populated

Expand Down Expand Up @@ -1285,7 +1284,7 @@ cdef class _Timedelta(timedelta):
warnings.warn(
"Timedelta.delta is deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self.value

Expand Down
21 changes: 10 additions & 11 deletions pandas/_libs/tslibs/timestamps.pyx
Expand Up @@ -6,7 +6,6 @@ construction requirements, we need to do object instantiation in python
(see Timestamp class below). This will serve as a C extension type that
shadows the python class, where we do any heavy lifting.
"""
import inspect
import warnings

cimport cython
Expand Down Expand Up @@ -257,7 +256,7 @@ cdef class _Timestamp(ABCTimestamp):
warnings.warn(
"Timestamp.freq is deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self._freq

Expand Down Expand Up @@ -369,7 +368,7 @@ cdef class _Timestamp(ABCTimestamp):
"In a future version these will be considered non-comparable. "
"Use 'ts == pd.Timestamp(date)' or 'ts.date() == date' instead.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return NotImplemented
else:
Expand Down Expand Up @@ -670,7 +669,7 @@ cdef class _Timestamp(ABCTimestamp):
"version. When you have a freq, use "
f"freq.{field}(timestamp) instead.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

@property
Expand Down Expand Up @@ -1176,7 +1175,7 @@ cdef class _Timestamp(ABCTimestamp):
"""
if self.nanosecond != 0 and warn:
warnings.warn("Discarding nonzero nanoseconds in conversion.",
UserWarning, stacklevel=find_stack_level(inspect.currentframe()))
UserWarning, stacklevel=find_stack_level())

return datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
Expand Down Expand Up @@ -1255,7 +1254,7 @@ cdef class _Timestamp(ABCTimestamp):
warnings.warn(
"Converting to Period representation will drop timezone information.",
UserWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

if freq is None:
Expand All @@ -1264,7 +1263,7 @@ cdef class _Timestamp(ABCTimestamp):
"In a future version, calling 'Timestamp.to_period()' without "
"passing a 'freq' will raise an exception.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)

return Period(self, freq=freq)
Expand Down Expand Up @@ -1456,7 +1455,7 @@ class Timestamp(_Timestamp):
"Timestamp.utcfromtimestamp(ts).tz_localize(None). "
"To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return cls(datetime.utcfromtimestamp(ts))

Expand Down Expand Up @@ -1692,7 +1691,7 @@ class Timestamp(_Timestamp):
"as a wall time, not a UTC time. To interpret as a UTC time, "
"use `Timestamp(dt64).tz_localize('UTC').tz_convert(tz)`",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
# Once this deprecation is enforced, we can do
# return Timestamp(ts_input).tz_localize(tzobj)
Expand All @@ -1709,7 +1708,7 @@ class Timestamp(_Timestamp):
"The 'freq' argument in Timestamp is deprecated and will be "
"removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
if not is_offset_object(freq):
freq = to_offset(freq)
Expand Down Expand Up @@ -2045,7 +2044,7 @@ timedelta}, default 'raise'
warnings.warn(
"Timestamp.freqstr is deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return self._freqstr

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

import inspect
from typing import (
Literal,
cast,
Expand Down Expand Up @@ -113,7 +112,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(inspect.currentframe()),
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -340,7 +339,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(inspect.currentframe()),
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -819,7 +818,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(inspect.currentframe()),
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -974,7 +973,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(inspect.currentframe()),
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -1267,7 +1266,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(inspect.currentframe()),
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down
3 changes: 1 addition & 2 deletions pandas/compat/_optional.py
@@ -1,7 +1,6 @@
from __future__ import annotations

import importlib
import inspect
import sys
import types
import warnings
Expand Down Expand Up @@ -165,7 +164,7 @@ def import_optional_dependency(
warnings.warn(
msg,
UserWarning,
stacklevel=find_stack_level(inspect.currentframe()),
stacklevel=find_stack_level(),
)
return None
elif errors == "raise":
Expand Down

0 comments on commit c677ae4

Please sign in to comment.