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
Utilities for pycharm code formatting (flake8 and black)

Pycharm External Tools Extentions to Pycharm code formatting tools. Currently supported are flake8 and black on a selected code block. Usage Flake8 [P

Haim Daniel 13 Nov 03, 2022
👻 Phantom types for Python

phantom-types Phantom types for Python will help you make illegal states unrepresentable and avoid shotgun parsing by enabling you to practice "Parse,

Anton Agestam 118 Dec 22, 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
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
Naming Convention checker for Python

PEP 8 Naming Conventions Check your code against PEP 8 naming conventions. This module provides a plugin for flake8, the Python code checker. (It repl

Python Code Quality Authority 411 Dec 23, 2022
Mypy stubs, i.e., type information, for numpy, pandas and matplotlib

Mypy type stubs for NumPy, pandas, and Matplotlib This is a PEP-561-compliant stub-only package which provides type information for matplotlib, numpy

Predictive Analytics Lab 194 Dec 19, 2022
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
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
Flake8 extension to provide force-check option

flake8-force Flake8 extension to provide force-check option. When this option is enabled, flake8 performs all checks even if the target file cannot be

Kenichi Maehashi 9 Oct 29, 2022
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
Backport Python 3.8+ typing utils & add issubtype & more

typing-utils Backport Python3.8+ typing utils & issubtype & more Install API issubtype get_origin get_args get_type_hints Install pip install typi

10 Nov 09, 2022
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
Pyright extension for coc.nvim

coc-pyright Pyright extension for coc.nvim Install :CocInstall coc-pyright Note: Pyright may not work as expected if can't detect project root correct

Heyward Fann 1.1k Jan 02, 2023
Pylint plugin for improving code analysis for when using Django

pylint-django About pylint-django is a Pylint plugin for improving code analysis when analysing code using Django. It is also used by the Prospector t

Python Code Quality Authority 544 Jan 06, 2023
A simple plugin that allows running mypy from PyCharm and navigate between errors

mypy-PyCharm-plugin The plugin provides a simple terminal to run fast mypy daemon from PyCharm with a single click or hotkey and easily navigate throu

Dropbox 301 Dec 09, 2022
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
It's not just a linter that annoys you!

README for Pylint - https://pylint.pycqa.org/ Professional support for pylint is available as part of the Tidelift Subscription. Tidelift gives softwa

Python Code Quality Authority 4.4k Jan 04, 2023
mypy plugin to type check Kubernetes resources

kubernetes-typed mypy plugin to dynamically define types for Kubernetes objects. Features Type checking for Custom Resources Type checking forkubernet

Artem Yarmoliuk 16 Oct 10, 2022
Mylint - My really simple rendition of how a linter works.

mylint My really simple rendition of how a linter works. This original version was written for my AST article. Since then I've added tests and turned

Tushar Sadhwani 2 Dec 29, 2021
An open-source, mini imitation of GitHub Copilot for Emacs.

Second Mate An open-source, mini imitation of GitHub Copilot using EleutherAI GPT-Neo-2.7B (via Huggingface Model Hub) for Emacs. This is a much small

Sam Rawal 238 Dec 27, 2022