Skip to content

error: Item "str" of "Union[str, bytes, date, datetime, timedelta, bool, int, float, complex, Timestamp, Timedelta]" has no attribute "copy" [union-attr] #453

Closed
@randolf-scholz

Description

@randolf-scholz

Using a tuple as the key for DataFrame.loc returns ScalarType.

from pandas import MultiIndex, DataFrame

index = MultiIndex.from_tuples([(0, 0), (0, 1)])
df = DataFrame(range(2), index=index)
key = (0, 0)
x = df.loc[key].copy()  # raises [union-attr] Item has no attribute "copy"

I think the issue is these lines, that seem to just flatout make wrong assumptions in case when the DataFrame is equipped with a MultiIndex:

@overload
def __getitem__(
self,
idx: tuple[int | StrLike | tuple[ScalarT, ...], int | StrLike],
) -> Scalar: ...
@overload
def __getitem__(
self,
idx: ScalarT
| tuple[IndexType | MaskType | _IndexSliceTuple, ScalarT | None]
| None,
) -> Series: ...

Please complete the following information:

  • python version 3.10.6
  • version of type checker mypy 0.991
  • version of installed pandas-stubs 1.5.2.221124

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