Mypy/typeshed stubs for Pandas

10,691

Solution 1

By now (March 2021) there is a dedicated pandas stub project in progress. It is however not developed by the pandas team (thanks to @remeus for pointing that out). The data-science-types project is deprecated. On GitHub they write:

⚠️ this project has mostly stopped development ⚠️

The pandas team and the numpy team are both in the process of integrating type stubs into their codebases, and we don't see the point of competing with them.

The NumPy stubs used to be in a seperate project, but now have been merged into the main git repo:

These stubs have been merged into NumPy, and all further development will happen in the NumPy main repo. We welcome your contributions there!

Solution 2

As of June 2020, pandas doesn't have stubs yet, but there is some progress in that direction. By now, you can use the data-science-types package (on GitHub) which provides stub files for pandas, numpy and matplotlib, though it's also a work in progress.

Solution 3

I have not yet found stubs for pandas. However, someone has created some for NumPy.

One interim option could be to define a custom type according to how mypy sees pandas objects. When I wrap a DataFrame object in reveal_type(df) and run mypy, it reveals that the recognized type signature is

Union[builtins.dict[Union[builtins.str, builtins.int], builtins.dict[Any, Any]], Any]

However it shows the same for a Series object, so it's not very accurate.

Share:
10,691
smontanaro
Author by

smontanaro

A Python guy.

Updated on June 29, 2022

Comments

  • smontanaro
    smontanaro about 2 years

    Just checking to see if anybody listening has already generated a sort-of-working set of mypy/typeshed stubs for `pandas`. I naively ran stubgen over the local pandas install which generated some errors. I can go with what I have to start with, but was hoping someone else had pushed the ball further. (Nothing obvious turned up on GitHub, though there is an old ticket for stubs.)