Mypy stubs, i.e., type information, for numpy, pandas and matplotlib

Overview

Mypy type stubs for NumPy, pandas, and Matplotlib

Join the chat at https://gitter.im/data-science-types/community

This is a PEP-561-compliant stub-only package which provides type information for matplotlib, numpy and pandas. The mypy type checker (or pytype or PyCharm) can recognize the types in these packages by installing this package.

NOTE: This is a work in progress

Many functions are already typed, but a lot is still missing (NumPy and pandas are huge libraries). Chances are, you will see a message from Mypy claiming that a function does not exist when it does exist. If you encounter missing functions, we would be delighted for you to send a PR. If you are unsure of how to type a function, we can discuss it.

Installing

You can get this package from PyPI:

pip install data-science-types

To get the most up-to-date version, install it directly from GitHub:

pip install git+https://github.com/predictive-analytics-lab/data-science-types

Or clone the repository somewhere and do pip install -e ..

Examples

These are the kinds of things that can be checked:

Array creation

import numpy as np

arr1: np.ndarray[np.int64] = np.array([3, 7, 39, -3])  # OK
arr2: np.ndarray[np.int32] = np.array([3, 7, 39, -3])  # Type error
arr3: np.ndarray[np.int32] = np.array([3, 7, 39, -3], dtype=np.int32)  # OK
arr4: np.ndarray[float] = np.array([3, 7, 39, -3], dtype=float)  # Type error: the type of ndarray can not be just "float"
arr5: np.ndarray[np.float64] = np.array([3, 7, 39, -3], dtype=float)  # OK

Operations

import numpy as np

arr1: np.ndarray[np.int64] = np.array([3, 7, 39, -3])
arr2: np.ndarray[np.int64] = np.array([4, 12, 9, -1])

result1: np.ndarray[np.int64] = np.divide(arr1, arr2)  # Type error
result2: np.ndarray[np.float64] = np.divide(arr1, arr2)  # OK

compare: np.ndarray[np.bool_] = (arr1 == arr2)

Reductions

import numpy as np

arr: np.ndarray[np.float64] = np.array([[1.3, 0.7], [-43.0, 5.6]])

sum1: int = np.sum(arr)  # Type error
sum2: np.float64 = np.sum(arr)  # OK
sum3: float = np.sum(arr)  # Also OK: np.float64 is a subclass of float
sum4: np.ndarray[np.float64] = np.sum(arr, axis=0)  # OK

# the same works with np.max, np.min and np.prod

Philosophy

The goal is not to recreate the APIs exactly. The main goal is to have useful checks on our code. Often the actual APIs in the libraries is more permissive than the type signatures in our stubs; but this is (usually) a feature and not a bug.

Contributing

We always welcome contributions. All pull requests are subject to CI checks. We check for compliance with Mypy and that the file formatting conforms to our Black specification.

You can install these dev dependencies via

pip install -e '.[dev]'

This will also install NumPy, pandas, and Matplotlib to be able to run the tests.

Running CI locally (recommended)

We include a script for running the CI checks that are triggered when a PR is opened. To test these out locally, you need to install the type stubs in your environment. Typically, you would do this with

pip install -e .

Then use the check_all.sh script to run all tests:

./check_all.sh

Below we describe how to run the various checks individually, but check_all.sh should be easier to use.

Checking compliance with Mypy

The settings for Mypy are specified in the mypy.ini file in the repository. Just running

mypy tests

from the base directory should take these settings into account. We enforce 0 Mypy errors.

Formatting with black

We use Black to format the stub files. First, install black and then run

black .

from the base directory.

Pytest

python -m pytest -vv tests/

Flake8

flake8 *-stubs

License

Apache 2.0

Comments
  • Update pandas read_csv and to_csv

    Update pandas read_csv and to_csv

    Hey! I updated pandas read_csv and to _csv, and also a small fix to pandas.Series (map function)

    There are some small changes made by black formatter, (was it bad formatted before or did I hve something wrong in my settings?)

    I would appreciate it if you could review this.

    opened by hellocoldworld 9
  • Support str and int as dtypes.

    Support str and int as dtypes.

    Extend the set of dtypes with the str and int literals.

    Note -- it would help to add some comments to describe the intended use of the _Dtype types -- it was hard for me to guess if I needed to also extend any of these.

    Fix for #73

    opened by rpgoldman 9
  • Add Series.sort_index() signature

    Add Series.sort_index() signature

    • [x] Adds Series.sort_index based on stable version documentation
    • [x] Fixes wrong order of arguments in Series.sort_values() (ascending should go before inplace)
    • [x] Adds missing arguments to Series.sort_values()
    opened by krassowski 8
  • Small additions to DataFrame and Series

    Small additions to DataFrame and Series

    I've made a few more additions to the stub, fleshing it out as I found I needed more for my work. I've corrected the issue I found - thanks again, thomkeh! - and hope that others can benefit from this work.

    Thank you!

    opened by ZHSimon 8
  • Flesh out pandas and numpy a bit more

    Flesh out pandas and numpy a bit more

    This is the result of testing data-science-types against another project I contribute to: https://github.com/jldbc/pybaseball

    • I added some common .gitignores for venv and vscode
    • I found a few Pandas tweaks to support functions and parameters that we are using.
      • Tweak DataFrame.apply, DataFrame.drop, DataFrame.merge, DataFrame.rank, DataFrame.reindex, DataFrame.replace
      • Add DataFrame.assign, DataFrame.filter
      • Tweak Series.rank
      • Add pandas.isnull
      • Tweak DataFrame.loc
    • A few changes to numpy as well
      • Allow tuples -> numpy.array
      • Tweak numpy.setdiff1d
      • Add numpy.cos, numpy.deg2rad, numpy.sin, numpy.cos

    Everything was done using the latest Pandas docs for reference to data types:

    https://pandas.pydata.org/pandas-docs/stable/reference/

    I also did my best to add tests to support the changes as well

    opened by TheCleric 7
  • Shelelena/pandas improvements

    Shelelena/pandas improvements

    Improvements in pandas DataFrame, DataFrameGroupBy and SeriesGroupBy

    • specify DataFrame.groupby
    • add DataFrameGroupBy.aggregate
    • adjust data type in DataFrame.__init__
    • add __getattr__ to get columns in DataFrame and DataFrameGroupBy
    • correct return type of DataFrameGroupBy.__getitem__
    • add some missing statistical methods to DataFrameGroupBy and SeriesGroupBy
    opened by Shelelena 7
  • Fix numpy arange overload

    Fix numpy arange overload

    Change order of start/stop to comply with numpy documentation (https://numpy.org/doc/stable/reference/generated/numpy.arange.html) and change data types to float.

    This is my first ever PR on github for a public repository, so please be gentle. If I need to clear anything up, please let me know.

    opened by Hvlot 5
  • Missing pandas.to_numeric

    Missing pandas.to_numeric

    The pandas stubs are missing pandas.to_numeric.

    I would like to do a PR but I'm not really sure where to start or how to write proper type hints for this, as I've only just started learning about python typing for the last few days. Any help would be much appreciated.

    opened by wwuck 5
  • Add support for Series and DF at methods

    Add support for Series and DF at methods

    Created _AtIndexer classes for Series and DataFrame and used them to type the corresponding at() methods.

    Partial solution to #74.

    This doesn't fully work, because it doesn't handle the possibility that a data frame will contain categorical (string) or integer data, instead of just float. I don't know how to do this.

    opened by rpgoldman 5
  • Add support for pandas.IntDtypes and pandas.UIntDtypes

    Add support for pandas.IntDtypes and pandas.UIntDtypes

    This adds support for pandas:

    • Int8Dtype
    • Int16Dtype
    • Int32DType
    • Int64Dtype
    • UInt8Dtype
    • UInt16Dtype
    • UInt32DType
    • UInt64Dtype

    As well as a slew of base classes.

    We'll see how the CI likes it, but for some reason on my local machine, mypy is saying it can't find any of the types, when they have been clearly added to the __init__.pyi in the pandas-stubs root.

    If that continues to be a problem, I may need advice on how to fix.

    opened by TheCleric 4
  • Will numpy stubs be removed after next numpy release?

    Will numpy stubs be removed after next numpy release?

    Numpy has finally merged the stubs from numpy-stubs into the main numpy project.

    https://github.com/numpy/numpy-stubs/pull/88 https://github.com/numpy/numpy/pull/16515

    Will the numpy stubs in this project be removed when numpy 1.20.0 is released?

    opened by wwuck 4
  • No overload variant of

    No overload variant of "subplots" matches argument type "bool"

    When I perform the following:

    from matplotlib.pyplot import subplots
    FIG, AXES = subplots(constrained_layout=True)
    

    I get the warning:

    No overload variant of "subplots" matches argument type "bool".

    Does that need to be added?

    opened by uihsnv 0
  • test_frame_iloc fails on Pandas 1.2

    test_frame_iloc fails on Pandas 1.2

    tests/pandas_test.py line 92 fails on Pandas 1.2

    Extracting the relevant code

    import pandas as pd
    df: pd.DataFrame = pd.DataFrame(
        [[1.0, 2.0], [4.0, 5.0], [7.0, 8.0]],
        index=["cobra", "viper", "sidewinder"],
        columns=["max_speed", "shield"],
    )
    s: "pd.Series[float]" = df["shield"].copy()
    df.iloc[0] = s
    

    Results in

    ValueError: could not broadcast input array from shape (3) into shape (2)
    

    This runs fine on Pandas 1.1.5

    opened by EdwardJRoss 0
  • Pandas `DataFrame.concat` missing some arguments

    Pandas `DataFrame.concat` missing some arguments

    The concat method for joining multiple DataFrames appears to be missing several arguments, such as join, keys, levels, and more.

    https://github.com/predictive-analytics-lab/data-science-types/blob/faebf595b16772d3aa70d56ea179a2eaffdbd565/pandas-stubs/init.pyi#L37-L42

    Compare to the Pandas docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

    opened by kevinhu 0
Releases(v0.2.23)
Owner
Predictive Analytics Lab
Predictive Analytics Lab
An enhanced version of the Python typing library.

typingplus An enhanced version of the Python typing library that always uses the latest version of typing available, regardless of which version of Py

Contains 6 Mar 26, 2021
A plugin for flake8 integrating Mypy.

flake8-mypy NOTE: THIS PROJECT IS DEAD It was created in early 2017 when Mypy performance was often insufficient for in-editor linting. The Flake8 plu

Łukasz Langa 103 Jun 23, 2022
Performant type-checking for python.

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providi

Facebook 6.2k Jan 04, 2023
Flake8 plugin to find commented out or dead code

flake8-eradicate flake8 plugin to find commented out (or so called "dead") code. This is quite important for the project in a long run. Based on eradi

wemake.services 277 Dec 27, 2022
A framework for detecting, highlighting and correcting grammatical errors on natural language text.

Gramformer Human and machine generated text often suffer from grammatical and/or typographical errors. It can be spelling, punctuation, grammatical or

Prithivida 1.3k Jan 08, 2023
Plugin for mypy to support zope.interface

Plugin for mypy to support zope.interface The goal is to be able to make zope interfaces to be treated as types in mypy sense. Usage Install both mypy

Shoobx 36 Oct 29, 2022
Reference implementation of sentinels for the Python stdlib

Sentinels This is a reference implementation of a utility for the definition of sentinel values in Python. This also includes a draft PEP for the incl

Tal Einat 22 Aug 27, 2022
Type annotations builder for boto3 compatible with VSCode, PyCharm, Emacs, Sublime Text, pyright and mypy.

mypy_boto3_builder Type annotations builder for boto3-stubs project. Compatible with VSCode, PyCharm, Emacs, Sublime Text, mypy, pyright and other too

Vlad Emelianov 2 Dec 05, 2022
Flake8 plugin for managing type-checking imports & forward references

flake8-type-checking Lets you know which imports to put in type-checking blocks. For the imports you've already defined inside type-checking blocks, i

snok 67 Dec 16, 2022
Enforce the same configuration across multiple projects

Nitpick Flake8 plugin to enforce the same tool configuration (flake8, isort, mypy, Pylint...) across multiple Python projects. Useful if you maintain

Augusto W. Andreoli 315 Dec 25, 2022
MyPy types for WSGI applications

WSGI Types for Python This is an attempt to bring some type safety to WSGI applications using Python's new typing features (TypedDicts, Protocols). It

Blake Williams 2 Aug 18, 2021
Typed interface stubs for Pythonista iOS

Pythonista Stubs Stubs for the Pythonista iOS API. This allows for better error detection and IDE / editor autocomplete. Installation and Usage pip in

Harold Martin 12 Jul 14, 2020
Mypy plugin and stubs for SQLAlchemy

Pythonista Stubs Stubs for the Pythonista iOS API. This allows for better error detection and IDE / editor autocomplete. Installation and Usage pip in

Dropbox 521 Dec 29, 2022
Pylint plugin to enforce some secure coding standards for Python.

Pylint Secure Coding Standard Plugin pylint plugin that enforces some secure coding standards. Installation pip install pylint-secure-coding-standard

Nguyen Damien 2 Jan 04, 2022
Utilities for refactoring imports in python-like syntax.

aspy.refactor_imports Utilities for refactoring imports in python-like syntax. Installation pip install aspy.refactor_imports Examples aspy.refactor_i

Anthony Sottile 20 Nov 01, 2022
Check for python builtins being used as variables or parameters

Flake8 Builtins plugin Check for python builtins being used as variables or parameters. Imagine some code like this: def max_values(list, list2):

Gil Forcada Codinachs 98 Jan 08, 2023
Tool for pinpointing circular imports in Python. Find cyclic imports in any project

Pycycle: Find and fix circular imports in python projects Pycycle is an experimental project that aims to help python developers fix their circular de

Vadim Kravcenko 311 Dec 15, 2022
A static type analyzer for Python code

pytype - 🦆 ✔ Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Google 4k Dec 31, 2022
Run isort, pyupgrade, mypy, pylint, flake8, and more on Jupyter Notebooks

Run isort, pyupgrade, mypy, pylint, flake8, mdformat, black, blacken-docs, and more on Jupyter Notebooks ✅ handles IPython magics robustly ✅ respects

663 Jan 08, 2023
Type stubs for the lxml package

lxml-stubs About This repository contains external type annotations (see PEP 484) for the lxml package. Installation To use these stubs with mypy, you

25 Dec 26, 2022