在 pandas.DataFrame.iterrows() 中为索引键入注释/提示

发布于 2025-01-10 05:08:40 字数 1748 浏览 2 评论 0原文

我正在尝试在 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)

正如我理解,mypyindex 视为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-01-17 05:08:40

您可以将 index 提示为 Optional[int],但是 x + 1 将不会进行类型检查。

我不确定 Union[typing.Hashable, None] 来自哪里; iterrows 本身返回一个 Iterable[tuple[Hashable, Series]]。但似乎您可以放心地断言,如果为 index 分配了一个值,那么它就不会是 None

index: Optional[int]
for index, row in someTable.iterrows():
    index = typing.cast(int, index)
    print(index + 1)

Union 是否应该反映可迭代引发 StopIteration 的可能性?这似乎不对,因为引发异常的函数不会返回 ;它根本不返回。)

You could hint index as Optional[int], but then x + 1 won't type check.

I'm not sure where Union[typing.Hashable, None] comes from; iterrows itself returns an Iterable[tuple[Hashable, Series]]. But it seems like you can safely assert that if index is assigned a value, then it will not be None.

index: Optional[int]
for index, row in someTable.iterrows():
    index = typing.cast(int, index)
    print(index + 1)

(Is the Union supposed to reflect the possibility of the iterable raising StopIteration? That doesn't seem right, as a function that raises an exception doesn't return None; it doesn't return at all.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文