Skip to content

DataFrame.loc does not like Index for columns #1299

@cmp0xff

Description

@cmp0xff

Describe the bug

No overload for DataFrame.loc[Series[bool], Sequence[Hashable]] or DataFrame.loc[Series[bool], Index[Hashable]]

To Reproduce

Provide a minimal runnable pandas example that is not properly checked by the stubs.

from collections.abc import Sequence
from typing import cast, reveal_type

import pandas as pd

a = cast(Sequence[str], ["a", "b"])
ind = pd.Index(a)

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
mask = pd.Series([True, False])

reveal_type(df.loc[mask, a])
reveal_type(df.loc[mask, ind])

Indicate which type checker you are using (mypy or pyright).

pyright

Show the error message received from that type checker while checking your example.

  loc.py:12:13 - warning: No overloads for "__getitem__" match the provided arguments (reportCallIssue)
  loc.py:12:13 - warning: Argument of type "tuple[Series[bool], Sequence[str]]" cannot be assigned to parameter "idx" of type "tuple[Scalar, slice[Any, Any, Any]]" in function "__getitem__"
    "tuple[Series[bool], Sequence[str]]" is not assignable to "tuple[Scalar, slice[Any, Any, Any]]"
      Tuple entry 1 is incorrect type
        Type "Series[bool]" is not assignable to type "Scalar"
          "Series[bool]" is not assignable to "str"
          "Series[bool]" is not assignable to "bytes"
          "Series[bool]" is not assignable to "date"
          "Series[bool]" is not assignable to "datetime"
          "Series[bool]" is not assignable to "timedelta" (reportArgumentType)
  loc.py:12:13 - information: Type of "df.loc[mask, a]" is "Unknown"
  loc.py:13:13 - warning: No overloads for "__getitem__" match the provided arguments (reportCallIssue)
  loc.py:13:13 - warning: Argument of type "tuple[Series[bool], Index[str]]" cannot be assigned to parameter "idx" of type "tuple[Scalar, slice[Any, Any, Any]]" in function "__getitem__"
    "tuple[Series[bool], Index[str]]" is not assignable to "tuple[Scalar, slice[Any, Any, Any]]"
      Tuple entry 1 is incorrect type
        Type "Series[bool]" is not assignable to type "Scalar"
          "Series[bool]" is not assignable to "str"
          "Series[bool]" is not assignable to "bytes"
          "Series[bool]" is not assignable to "date"
          "Series[bool]" is not assignable to "datetime"
          "Series[bool]" is not assignable to "timedelta" (reportArgumentType)
  loc.py:13:13 - information: Type of "df.loc[mask, ind]" is "Unknown"

Please complete the following information

  • OS: Windows
  • OS Version: 11
  • python version: 3.13.4
  • version of type checker: 1.1.403
  • version of installed pandas-stubs: 2.3.0.250703

Additional context

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions