-
Notifications
You must be signed in to change notification settings - Fork 102
Python: Enhance completions of DataFrames with column information #601
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
Comments
Google document with more detail on this feature. |
Some findings so far: Pandas DataFrame columns are already accessible as attributes, for example: In [1]: import pandas as pd
In [2]: df = pd.DataFrame({"mycolumn": [7]})
In [3]: df
Out[3]:
mycolumn
0 7 So I tried to see whether completing I'm now digging into why that's the case. |
I've narrowed it down with the script below. It seems that if Jedi is able to statically analyse the code snippet it receives, it prefers that to any existing variables in the passed import pandas as pd
from jedi.api import Interpreter
df = pd.DataFrame({'mycolumn': [7]})
namespaces = [{'df': df}]
# These completions include the `mycolumn` attribute.
code = 'df.'
interpreter = Interpreter(code, namespaces)
completions = interpreter.complete(1, 3)
print([completion.complete for completion in completions if completion.complete == 'mycolumn'])
# ['mycolumn']
# These completions don't include the `mycolumn` attribute.
# The only difference is that `code` contains the import and definition of `df`, which seems to
# make Jedi prefer static analysis and ignore the namespace
code = '''
import pandas as pd
df = pd.DataFrame({'mycolumn': [7]})
df.
'''.strip()
interpreter = Interpreter(code, namespaces)
completions = interpreter.complete(3, 3)
print([completion.complete for completion in completions if completion.complete == 'mycolumn'])
# [] |
Marking this as "blocked" while we wait for a response on our issue in the Jedi repo. If that doesn't happen soon I'll look into a workaround. |
The Jedi maintainers' stance is that this case is too specific for them to handle. Some thoughts on solutions when this is picked up again:
|
This is ready for review. You should now be able to complete the columns of pandas and polars dataframes via dict access (e.g. Secondly, you should also see a preview of a dataframe/series in the completion documentation pop-up window. Here's a snippet to create pandas and polars dataframes: import numpy as np, pandas as pd, polars as pl
data = np.random.randn(100, 3)
pd_df = pd.DataFrame({k: v for k, v in zip('abcdefghijklmnopqrstuvwxyz', data)})
pl_df = pl.DataFrame({k: v for k, v in zip('abcdefghijklmnopqrstuvwxyz', data)}) |
I think there's a regression in Jedi after merging latest upstream for completions, will chat with you on what I see in the output channel this coming week. |
I don't think this is related to our changes. I'm not 100% sure but I suspect it's related to upstream changes around managing the Here is the corresponding upstream issue: microsoft/vscode-python#22659. Here are the logs:
|
This is ready for review. I believe the error mentioned above is unrelated - this still works for me in the latest build. |
In Positron 2023.12.0 (Universal) build 1644 I can get column completions via dict access ( columns.movI haven't worked a ton with polars but looks like "columns as attributes" is not a thing, right? I believe so, so I am closing as complete. ✅ |
@juliasilge yep, polars doesn't do "columns as attributes" |
Merge pull request #265 from posit-dev/prefer-namespace-completions Prefer namespace completions -------------------- Commit message for posit-dev/positron-python@8359792: test that namespace completions are preferred -------------------- Commit message for posit-dev/positron-python@1d3855a: prefer completions using the user's namespace over static analysis Relates to #601. Authored-by: Wasim Lorgat <[email protected]> Signed-off-by: Wasim Lorgat <[email protected]>
Merge pull request #265 from posit-dev/prefer-namespace-completions Prefer namespace completions -------------------- Commit message for posit-dev/positron-python@8359792: test that namespace completions are preferred -------------------- Commit message for posit-dev/positron-python@1d3855a: prefer completions using the user's namespace over static analysis Relates to #601. Authored-by: Wasim Lorgat <[email protected]> Signed-off-by: Wasim Lorgat <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Positron uses Jedi for its Python LSP. We want to enhance completion suggestions provided by the LSP to include more information about the objects in the user's environment. This requires that we connect the information from the user's session in the ipython kernel with the LSP.
One set of enhancements we want to make are that completions for DataFrame objects include the dynamic column names of the data.
The text was updated successfully, but these errors were encountered: