Skip to content
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

"Expected type 'str', got 'Series | Series' instead" Warning When Accessing DataFrame Cell in Pandas #856

Closed
JasonMendoza2008 opened this issue Jan 25, 2024 · 5 comments

Comments

@JasonMendoza2008
Copy link
Contributor

JasonMendoza2008 commented Jan 25, 2024

I am working with a Pandas DataFrame in Python and encountering an issue when trying to access a specific cell value. My goal is to retrieve the city name associated with a particular row in the DataFrame.

Minimal Reproducible Example:

import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

selected_cell: str = df.loc[1]['City']

When I run this code, I receive the following warning:
Expected type 'str', got 'Series | Series' instead

I don't think the type-hinting is wrong.

selected_cell: str = df.loc[1, "City"] does not work either.

I'm running on Python 3.11.1 and pandas 2.1.0 and pandas-stubs 2.0.0.230412. The type-checker is PyCharm 2023.3.2 (Professional Edition).

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 25, 2024

Which type checker are you using?

What you want to use here is selected_cell: str = df.loc[1, "City"]. That will return a scalar value

@JasonMendoza2008
Copy link
Contributor Author

JasonMendoza2008 commented Jan 26, 2024

It's PyCharm checker. PyCharm 2023.3.2 (Professional Edition). Sorry I hadn't included it initially. I've edited the issue.

selected_cell: str = df.loc[1, "City"] doesn't work either.

image

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 26, 2024

I think that's a bug in the pycharm type checker. I tried your code with both pyright and mypy and it works fine.

You might also considering upgrading your version of pandas-stubs to see if that helps. Last release is 2.1.4.231227

@JasonMendoza2008
Copy link
Contributor Author

I updated pandas-stubs and still have the same problem. I think it's a PyCharm problem then and I should open up an issue on their end?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 29, 2024

I updated pandas-stubs and still have the same problem. I think it's a PyCharm problem then and I should open up an issue on their end?

Yes. I'm going to close this as we are only supporting pyright and mypy and your example passes both type checkers.

@Dr-Irv Dr-Irv closed this as completed Jan 29, 2024
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

2 participants