79223399

Date: 2024-11-25 14:18:38
Score: 2
Natty:
Report link

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.

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.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): have the same issue
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: PeterH