在 pandas.DataFrame.iterrows() 中为索引键入注释/提示
我正在尝试在 Python 脚本中添加类型注释/提示以运行 mypy 检查。我有一个 pandas.DataFrame
对象,我iterate 像这样:
someTable: pandas.DataFrame = pandas.DataFrame()
# ...
# adding some data to someTable
# ...
for index, row in someTable.iterrows():
#reveal_type(index)
print(type(index))
print(index + 1)
如果我运行这个脚本,这就是我得到的:
$ python ./some.py
<class 'int'>
2
<class 'int'>
3
如果我用 mypy
检查它,那么它会报告错误:
$ mypy ./some.py
some.py:32: note: Revealed type is "Union[typing.Hashable, None]"
some.py:34: error: Unsupported operand types for + ("Hashable" and "int")
some.py:34: error: Unsupported operand types for + ("None" and "int")
some.py:34: note: Left operand is of type "Optional[Hashable]"
Found 2 errors in 1 file (checked 1 source file)
正如我理解,mypy
将 index
视为 Union[typing.Hashable, None]
,它不是 int
,并且所以 index + 1
看起来像是一个错误。然后我应该如何以及在哪里注释/提示它以满足mypy
?
我尝试过这个:
index: int
for index, row in someTable.iterrows():
# ...
但这会导致:
$ mypy ./some.py
some.py:32: error: Incompatible types in assignment (expression has type "Optional[Hashable]", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
I am trying to add type annotations/hints in a Python script for running mypy checks. I have a pandas.DataFrame
object, which I iterate like this:
someTable: pandas.DataFrame = pandas.DataFrame()
# ...
# adding some data to someTable
# ...
for index, row in someTable.iterrows():
#reveal_type(index)
print(type(index))
print(index + 1)
If I run this script, here's what I get:
$ python ./some.py
<class 'int'>
2
<class 'int'>
3
And if I check it with mypy
, then it reports errors:
$ mypy ./some.py
some.py:32: note: Revealed type is "Union[typing.Hashable, None]"
some.py:34: error: Unsupported operand types for + ("Hashable" and "int")
some.py:34: error: Unsupported operand types for + ("None" and "int")
some.py:34: note: Left operand is of type "Optional[Hashable]"
Found 2 errors in 1 file (checked 1 source file)
As I understand, mypy
sees the index
as Union[typing.Hashable, None]
, which is not int
, and so index + 1
looks like an error to it. How and where should I then annotate/hint it to satisfy mypy
?
I tried this:
index: int
for index, row in someTable.iterrows():
# ...
but that results in:
$ mypy ./some.py
some.py:32: error: Incompatible types in assignment (expression has type "Optional[Hashable]", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
index
提示为Optional[int]
,但是x + 1
将不会进行类型检查。我不确定
Union[typing.Hashable, None]
来自哪里;iterrows
本身返回一个Iterable[tuple[Hashable, Series]]
。但似乎您可以放心地断言,如果为index
分配了一个值,那么它就不会是None
。(
Union
是否应该反映可迭代引发StopIteration
的可能性?这似乎不对,因为引发异常的函数不会返回无
;它根本不返回。)You could hint
index
asOptional[int]
, but thenx + 1
won't type check.I'm not sure where
Union[typing.Hashable, None]
comes from;iterrows
itself returns anIterable[tuple[Hashable, Series]]
. But it seems like you can safely assert that ifindex
is assigned a value, then it will not beNone
.(Is the
Union
supposed to reflect the possibility of the iterable raisingStopIteration
? That doesn't seem right, as a function that raises an exception doesn't returnNone
; it doesn't return at all.)