[Python] Accessing doc strings from Spyder

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    It's hard to pull up the doc string of some objects in Spyder. In this particular case, I tried various ways to access the documentation for a pandas DataFrame and GroupBy object:

    >>> import pandas as pd
    >>> df = pd.DataFrame([[1,2],[3,4]],columns=['A','B'])
    >>> gb = df.groupby('A')


    In the Spyder console, pressing Ctrl+I after df pulls up the documentation, but pressing Ctrl+I after gb generates the message "No documentation available".

    Issuing help(df) and help(gb) are not helpful because the console has no pager and the quanity of output text exceeds the console buffer size.

    I tried accessing the source file, which works for pd.DataFrame but not df nor gb:

    >>> import inspect
    >>> inspect.getsourcefile(pd.DataFrame)

    'C:\\Users\\User.Name\\AppData\\Local\\anaconda3\\envs\\py39\\lib\\site-packages\\pandas\\core\\frame.py'

    >>> inspect.getsourcefile(df)

    TypeError: module, class, method, function,
    traceback, frame, or code object was
    expected, got DataFrame

    >>> inspect.getsourcefile(gb)

    TypeError: module, class, method, function,
    traceback, frame, or code object was
    expected, got DataFrameGroupBy


    From everything I've found online, getsourcefile() should accept an object, so it is unclear to me why pd.DataFrame is accepted but not df.

    I know that the source file for DataFrameGroupBy is available and accessible. I found my way to it in a very round-about manner.

    First, I found how the full class path for df matched the path of the source file:

    >>> type(df)

    pandas.core.frame.DataFrame


    Since the getsourcefile reported source file frame.py for pd.DataFrame above, it is clear that the last component is the class name and the 2nd last component is the base name of the source file.

    Inspecting frame.py I saw that it imports DataFrameGroupBy and noted the "path":

    from pandas.core.groupby.generic import DataFrameGroupBy


    The .pandas.core. component is the same as its occurrence in the full path to frame.py, so from \\pandas\\core\\, I descended into the subfolders for the ensuing path components groupby\\generic.py. It did indeed contain the definition class DataFrameGroupBy. I subsequently found that I could have found the path to the source file for DataFrameGroupBy from its type:

    >>> type(gb)

    pandas.core.groupby.generic.DataFrameGroupBy


    After all of that, my two related questions are:

    (1) Why didn't getsourcefile find the source file for DataFrameGroupBy?

    (2) Is there an easier way to peruse the documentation in Spyder?

    Continue reading...

Compartilhe esta Página