pandas 熊猫的 Pythonic 类型提示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43890844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pythonic type hints with pandas?
提问by dangom
Let's take a simple function that takes a str and returns a dataframe:
让我们看一个简单的函数,它接受一个 str 并返回一个数据帧:
import pandas as pd
def csv_to_df(path):
return pd.read_csv(path, skiprows=1, sep='\t', comment='#')
What is the recommended pythonic way of adding type hints to this function?
向此函数添加类型提示的推荐 Pythonic 方法是什么?
If I ask python for the type of a DataFrame it returns pandas.core.frame.DataFrame
.
The following won't work though, as it'll tell me that pandas is not defined.
如果我向 python 询问 DataFrame 的类型,它返回pandas.core.frame.DataFrame
. 但是,以下内容不起作用,因为它会告诉我未定义Pandas。
def csv_to_df(path: str) -> pandas.core.frame.DataFrame:
return pd.read_csv(path, skiprows=1, sep='\t', comment='#')
回答by Georgy
Why not just use pd.DataFrame
?
为什么不直接使用pd.DataFrame
?
import pandas as pd
def csv_to_df(path: str) -> pd.DataFrame:
return pd.read_csv(path, skiprows=1, sep='\t', comment='#')
Result is the same:
结果是一样的:
> help(csv_to_df)
Help on function csv_to_df in module __main__:
csv_to_df(path:str) -> pandas.core.frame.DataFrame
回答by dangom
I'm currently doing the following:
我目前正在做以下事情:
from typing import TypeVar
PandasDataFrame = TypeVar('pandas.core.frame.DataFrame')
def csv_to_df(path: str) -> PandasDataFrame:
return pd.read_csv(path, skiprows=1, sep='\t', comment='#')
Which gives:
这使:
> help(csv_to_df)
Help on function csv_to_df in module __main__:
csv_to_df(path:str) -> ~pandas.core.frame.DataFrame
Don't know how pythonic that is, but it's understandable enough as a type hint, I find.
不知道那是多么pythonic,但我发现它作为类型提示是可以理解的。
回答by Keith
This is straying from the original question but building off of @dangom's answer using TypeVar
and @Georgy's comment that there is no way to specify datatypes for DataFrame columns in type hints, you could use a simple work-around like this to specify datatypes in a DataFrame:
这与原始问题背道而驰,但基于@dangom 的回答使用TypeVar
和@Georgy 的评论,即无法在类型提示中为 DataFrame 列指定数据类型,您可以使用这样的简单解决方法来指定 DataFrame 中的数据类型:
from typing import TypeVar
DataFrameStr = TypeVar("pandas.core.frame.DataFrame(str)")
def csv_to_df(path: str) -> DataFrameStr:
return pd.read_csv(path, skiprows=1, sep='\t', comment='#')