Skip to content

Index arithmetic overloads show wrong types #1347

@MarcoGorelli

Description

@MarcoGorelli

Describe the bug

I think this is an issue:

def __sub__(self, other: Any) -> Self: ...

as the dtype isn't preserved

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
  2. Indicate which type checker you are using (mypy or pyright).
  3. Show the error message received from that type checker while checking your example.
from __future__ import annotations
import pandas as pd
from typing import reveal_type

idx = pd.Index([1,2,3])

reveal_type(idx - 1.5)

at runtime this gives

Index([-0.5, 0.5, 1.5], dtype='float64')

but it's revealed to be Index[int] by type checkers

Please complete the following information:

  • OS: [e.g. Windows, Linux, MacOS]
  • OS Version [e.g. 22]
  • python version
  • version of type checker
  • version of installed pandas-stubs 3033eea

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexRelated to the Index class or subclassesNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions