Skip to content

ENH: Use find_stack_level universally #44347

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

Closed
rhshadrach opened this issue Nov 7, 2021 · 2 comments
Closed

ENH: Use find_stack_level universally #44347

rhshadrach opened this issue Nov 7, 2021 · 2 comments
Labels
Enhancement Warnings Warnings that appear or should be added to pandas

Comments

@rhshadrach
Copy link
Member

rhshadrach commented Nov 7, 2021

It can be difficult to determine where warnings are coming from in user code when the stacklevel used points to pandas internals. There were recent improvements (~6 months ago) to find_stack_level. Doing some time tests, I get the following results on my machine:

  • Depth of 3: 0.149ms
  • Depth of 100: 1.48ms
Timing code
    # Added as a method of DataFrame
    def foo(self, depth, runs):
        if depth > 0:
            self.foo(depth-1, runs)
        else:
            import time
            from pandas.util._exceptions import find_stack_level

            timer = time.time()
            for _ in range(runs):
                find_stack_level()
            print(f'Average runtime: {(time.time() - timer) / runs : 0.10f}')

pd.DataFrame().foo(depth=100, runs=1000)

Is this reliable/performant enough to use universally in all warnings so that the stacklevel is always correct?

@rhshadrach rhshadrach added Enhancement Needs Discussion Requires discussion from core team before further action Warnings Warnings that appear or should be added to pandas labels Nov 7, 2021
@jreback
Copy link
Contributor

jreback commented Nov 7, 2021

yes this is find to do universally - we don't generally have warnings in performance sensitive code (though of course should be careful)

@lithomas1 lithomas1 removed the Needs Discussion Requires discussion from core team before further action label Nov 11, 2021
@rhshadrach
Copy link
Member Author

rhshadrach commented Dec 1, 2022

This is now used universally. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

3 participants