Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Create a pandas.api.extensions.register_dataframe_accessor, then when using try to command + click in VSCode to jump to the definition. Either no definition is found or it jumps to a file like series.pyi
Create a pandas.api.extensions.register_dataframe_accessor, then when using try to command + click in VSCode to jump to the definition. Either no definition is found or it jumps to a file like series.pyi
my_utils/my_accessor.py
import pandas as pd
@pd.api.extensions.register_dataframe_accessor("demo")
class DemoAccessor:
def __init__(self, pandas_obj):
self._obj = pandas_obj
def say_hello(self):
print("Hello from accessor!")
main.py
import pandas as pd
import sys
import importlib
# Ensure the path to the module is in sys.path
sys.path.append("my_utils") # Adjust this path as needed
import my_accessor
importlib.reload(my_accessor)
# Create DataFrame and use accessor
df = pd.DataFrame({"A": [1, 2, 3]})
df.demo.say_hello() # This runs fine, but "jump to definition" doesn't work
Issue Description
VSCode go to definition doesn't work with pandas.api.extensions.register_dataframe_accessor
Expected Behavior
I can jump to the definition when command clicking and see the documentation in VSCode
Installed Versions
Comment From: rhshadrach
Thanks for the report. Further investigations are certainly welcome. If there is an adjustment pandas can make to improve this functionality and it isn't a heavy code change, then a PR is welcome as well. However I do not think we should leave issues for supporting various IDEs on the queue for the long term if there is no proposed fix. Marking as a closing candidate for now.
Comment From: iabhi4
I looked into this a bit more to see if there's a clean way to get “Go to Definition” working in VSCode (Pylance) for @register_dataframe_accessor
.
After trying things like __annotations__
and TYPE_CHECKING
, what actually worked was:
Creating a subclass of DataFrame
with the accessor annotated:
class DataFrameWithAccessor(pd.DataFrame):
demo: DemoAccessor
Then doing a cast before usage:
df = pd.DataFrame({...})
df = cast(DataFrameWithAccessor, df)
df.demo.say_hello()
This makes Pylance resolve the method statically without needing anything from pandas itself. Might be worth dropping a line about this in the docs or a dev tip somewhere, happy to PR it if useful.
Comment From: mroeschke
Agreed that pandas shouldn't necessarily be responsible for IDE specific feature support, so closing
Comment From: arnoldjames98
For other with this problem, here's a link to another thread with the solution that worked for me: https://github.com/microsoft/pylance-release/issues/7275.