Skip to content

PERF: find_stack_level #45247

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 1 commit into from
Jan 7, 2022
Merged
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
11 changes: 7 additions & 4 deletions pandas/util/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ def find_stack_level() -> int:
Find the first place in the stack that is not inside pandas
(tests notwithstanding).
"""
stack = inspect.stack()

import pandas as pd

pkg_dir = os.path.dirname(pd.__file__)
test_dir = os.path.join(pkg_dir, "tests")

for n in range(len(stack)):
fname = stack[n].filename
# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
frame = inspect.currentframe()
n = 0
while frame:
fname = inspect.getfile(frame)
if fname.startswith(pkg_dir) and not fname.startswith(test_dir):
continue
frame = frame.f_back
n += 1
else:
break
return n