Skip to content

s.str.upper doesn't preserve s type #1149

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

Open
MarcoGorelli opened this issue Mar 7, 2025 · 2 comments
Open

s.str.upper doesn't preserve s type #1149

MarcoGorelli opened this issue Mar 7, 2025 · 2 comments

Comments

@MarcoGorelli
Copy link
Member

import pandas as pd

s: "pd.Series[str]" = pd.Series(['a', 'b'])
reveal_type(s)  # information: Type of "s" is "Series[str]"
reveal_type(s.str.upper())  # information: Type of "s.str.upper()" is "Series[Unknown]"

The reason I'm reporting this is that the source code uses a TypeVar, so the intention might have been that the type be preserved?

def upper(self) -> T: ...

@loicdiridollou
Copy link
Contributor

The issue here is less about the T but I think is closer to the str property which does not preserve the type of s.
If you try to highlight the type of s.str you realize that it has lost the information about whether it is a Series[str] or anything else.
I can’t remember how to properly type hint properties but that may be an idea.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 7, 2025

I think that the declaration of str in Series should add an argument to StringMethods related to the subtype of Index or Series, i.e., include S1 in that signature so that the bound value of S1 gets passed down. At least that's my hunch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants