Skip to content

[call-overload] Series.name is incompatible with DataFrame.__getitem__ #570

@randolf-scholz

Description

@randolf-scholz

Describe the bug

It should be type-safe to put Series.name into DataFrame.__getitem__, but Series.name is defined as None | Hashable and DataFrame.__getitem__ does not accept Hashable.

@overload
def __getitem__(self, idx: Scalar | tuple[Hashable, ...]) -> Series: ...
@overload
def __getitem__(self, rows: slice) -> DataFrame: ...
@overload
def __getitem__(
self,
idx: Series[_bool]
| DataFrame
| Index
| np_ndarray_str
| np_ndarray_bool
| list[_ScalarOrTupleT],
) -> DataFrame: ...

@property
def name(self) -> Hashable | None: ...

To Reproduce

from pandas import DataFrame, Series

foo = Series([1, 2, 3], name="foo")
bar = DataFrame(columns=["foo", "bar", "baz"])
bar[foo.name]  # [call-overload]
# No overload variant of "__getitem__" of "DataFrame" matches argument type "Hashable" 

Please complete the following information:

  • pandas 1.5.3
  • pandas-stubs 1.5.3.230304

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions