Skip to content

Series.keys() should return an Index and not a list #1101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ClementPinard opened this issue Jan 21, 2025 · 2 comments
Closed

Series.keys() should return an Index and not a list #1101

ClementPinard opened this issue Jan 21, 2025 · 2 comments
Labels
good first issue Series Series data structure

Comments

@ClementPinard
Copy link
Contributor

Describe the bug

a = pd.Series({0:0, 1:1})
reveal_type(a.keys()
print(type(a.keys())

Running pyright on this code gives you

information: Type of "a.keys()" is "list[Unknown]"

Which is normal since it's written explictly here :

def keys(self) -> list: ...

However, if we run it with python, we get pandas.core.indexes.base.Index

Should we replace the linked line from list to Index ?

I have a code that is failing type checking because it call a.keys().tolist(), while it does work as intended.

@ClementPinard ClementPinard changed the title Series.keys() should return an Index and not a series Series.keys() should return an Index and not a list Jan 21, 2025
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 31, 2025

Thanks for the report.

PR with tests welcome.

@Dr-Irv Dr-Irv added good first issue Series Series data structure labels Jan 31, 2025
janlodewijk added a commit to janlodewijk/pandas-stubs that referenced this issue Feb 6, 2025
Imported Index from pandas
and
Changed keys method:
def keys(self) -> Index: ...
Dr-Irv added a commit that referenced this issue Feb 10, 2025
* Update series.pyi, tackling issue #1101

Imported Index from pandas
and
Changed keys method:
def keys(self) -> Index: ...

* Update test_series.py

Add a test for issue # 1101

* Update tests/test_series.py

Following the pandas pattern.

Co-authored-by: Irv Lustig <[email protected]>

* Fix import issue in series.pyi to resolve Ruff error

* Delete test

---------

Co-authored-by: Irv Lustig <[email protected]>
@ClementPinard
Copy link
Contributor Author

Closing, as I do believe it has been solved, thanks @janlodewijk !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Series Series data structure
Projects
None yet
Development

No branches or pull requests

2 participants