Skip to content

at and loc don't accept index of the dataframe itself #1365

@gothicVI

Description

@gothicVI

Describe the bug
The following code works but mypy complains with

Invalid index type "tuple[Hashable, str]" for "_AtIndexerFrame"; expected type "Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | str_ | <9 more items>"  [index]

and pyright comlains with:

error: Argument of type "tuple[Hashable, Literal['uuid']]" cannot be assigned to parameter "idx" of type "Series[bool] | np_ndarray_bool | list[bool] | str | str_ | _IndexSliceTuple[Unknown] | list[ScalarT@__setitem__] | int | int_ | integer[Any] | unsignedinteger[Any] | signedinteger[Any] | int8 | slice[Any, Any, Any]" in function "__setitem__"
    Type "tuple[Hashable, Literal['uuid']]" is not assignable to type "Series[bool] | np_ndarray_bool | list[bool] | str | str_ | _IndexSliceTuple[Unknown] | list[ScalarT@__setitem__] | int | int_ | integer[Any] | unsignedinteger[Any] | signedinteger[Any] | int8 | slice[Any, Any, Any]"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "int"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "str"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "str_"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "Series[bool]"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "integer[Any]"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "list[ScalarT@__setitem__]"
      "tuple[Hashable, Literal['uuid']]" is not assignable to "list[bool]" (reportArgumentType)

To Reproduce

import pandas as pd
df = pd.DataFrame({"A": ["B", "B"]})
print(df)
#    A
# 0  B
# 1  B
for index, row in df.iterrows():
    df.at[index, "A"] = "C"
print(df)
#    A
# 0  C
# 1  C

The same happens if loc is used. Adding

assert isinstance(index, int)

before assigning solves it.

Please complete the following information:

  • OS: Linux
  • OS Version: Debian 12
  • python 3.11
  • version of type checker: mypy & pyright
  • version of installed pandas-stubs: pandas 2.3.2 and pandas-stubs 2.3.2.250827

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions