Pythonic type hints with pandas?

后端 未结 5 1951
名媛妹妹
名媛妹妹 2020-12-24 04:36

Let\'s take a simple function that takes a str and returns a dataframe:

import pandas as pd
def csv_to_df(path):
    return pd.read_csv(path, skiprows=1, sep         


        
5条回答
  •  萌比男神i
    2020-12-24 05:21

    Check out the answer given here which explains the usage of the package data-science-types.

    pip install data-science-types
    

    Demo

    # program.py
    
    import pandas as pd
    
    df: pd.DataFrame = pd.DataFrame({'col1': [1,2,3], 'col2': [4,5,6]}) # OK
    df1: pd.DataFrame = pd.Series([1,2,3]) # error: Incompatible types in assignment
    

    Run using mypy the same way:

    $ mypy program.py

提交回复
热议问题