pandas-dev/pandas-stubs

`pd.core.frame.set_index` stubbing is not accurate

stdedos opened this issue · 5 comments

@overload
def set_index(
self,
keys: Label
| Series
| Index
| np.ndarray
| Iterator[HashableT]
| list[HashableT],
*,
drop: _bool = ...,
append: _bool = ...,
verify_integrity: _bool = ...,
inplace: Literal[True],
) -> None: ...
@overload
def set_index(
self,
keys: Label
| Series
| Index
| np.ndarray
| Iterator[HashableT]
| list[HashableT],
*,
drop: _bool = ...,
append: _bool = ...,
verify_integrity: _bool = ...,
inplace: Literal[False] = ...,
) -> DataFrame: ...

#154 had the right idea, but then #151 sorta-reverted it

Is there plans to partially revert at least these lines back to what they were?

Can you please provide an example that doesn't work for mypy/pyright?

edit:

#154 had the right idea, but then #151 sorta-reverted it

This PR only replaced Union with | for DataFrame.set_index but did otherwise not change anything about DataFrame.set_index.

... oh. You are right 😅

e.g., for this:

"""Test `pd.set_index`."""

import pandas as pd

# Assume df is your DataFrame
data = {"Year": [2000, 2001, 2002, 2003], "Quarter": [1, 2, 3, 4], "Revenue": [200, 220, 250, 275]}
df = pd.DataFrame(data)

# Create a multi-index using Year and Quarter
df.set_index(["Year", "Quarter"], inplace=True)

# Now you can perform more complex queries
q2_revenue = df.loc[(2001, 2), "Revenue"]
print(q2_revenue)  # Output: 220


print(df)

my IDE complains

image

... but not mypy:

$ mypy test.py
Success: no issues found in 1 source file

my IDE complains

Do you know which type checker your IDE is using?

It's PyCharm. So ... it uses a combination of "it's own stuff" and typeshed. Which "somehow" is hardcoded and not "auto-fetched+updated".

https://youtrack.jetbrains.com/issue/PY-63391

It's PyCharm. So ... it uses a combination of "it's own stuff" and typeshed. Which "somehow" is hardcoded and not "auto-fetched+updated".

Note that we only test the validity of the stubs against mypy and pyright. I believe we have heard reports that the type checker within PyCharm has errors, as compared to those other 2 type checkers.