Skip to content

Incorrect type when indexing a pandas dataframe through .loc and selecting a single column #1643

@diego-pm

Description

@diego-pm

Summary

I have been searching for issues related to this but haven't found this case. Some pandas related errors mentioned in the issues stated that it was due to ty not supporting TypeAlias, but the current version 0.0.1-alpha.28 does support it (and I have seen many errors related to pandas disappear in my codebase due to this but not this one). Maybe the error is due to a missing feature in ty that I am not aware of.

The issue is that when selecting a column trought .loc in a pandas dataframe, the infered type should be Series, but ty infers a DataFrame.

from typing_extensions import reveal_type
import pandas as pd

def foo(s: pd.Series[int], df: pd.DataFrame):
    reveal_type(df.loc[[1, 2], "col1"]) # revealed type: DataFrame

I have tested this also with pyright and it correctly reveals that the type is Series.

Setup:

Used ty check file.py, the default configuration was not modified.

pandas version: 2.2.1
pandas-stubs version: 2.2.1.240316
python version: 3.10.16

Version

ty 0.0.1-alpha.28

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions