Search code examples
pythonpandaspycharmjetbrains-ide

Missing Pandas methods in PyCharm (JetBrains) code completion


import pandas as pd

series = pd.Series([])
series.str.extract("")

While writing the above code, I expect PyCharm to start completing series.str.ex... with the available methods. It does not.

Example of what I am expecting using pd...: pd code completion

What actually happens:

series.str. completion

This works in VSCode, and works with most other libraries.

PyCharm 2024.3


Solution

  • Answer: Install pandas-stubs.

    Why: In pandas/core/series the string method is defined as

        str = CachedAccessor("str", StringMethods)
    

    CachedAccessor is a "Custom property-like object". There are no defined types in either pandas/core/series or CachedAccessor. Changing the str line to

    str: StringMethods = CachedAccessor("str", StringMethods) 
    

    solves the issue. This indicates the issue really is that the language server can not figure out what the str method should be.

    In python, there are two main ways to add types to code.

    • Add them inline IE, str: StringMethod
    • Add them in a stub only package in accordance to PEP 561

    Pandas has chosen to go the PEP 561 route and offers a stub only package pandas-stubs. The reason VSCode does not have the same issue pycharm does is Pylance ( the VSCode python language server) includes the pandas-stubs package out of the box.

    The stubs are tested with mypy and pyright and are currently shipped with the Visual Studio Code extension pylance.

    For more information, you can also check out the Pylance release notes and issue tracker.