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

Fix: DataFrame.replace incompatible with type "dict[str, dict[int, int]]" #1164

Merged
merged 3 commits into from
Mar 12, 2025

Conversation

MarcoGorelli
Copy link
Member

  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

closes #1161

@MarcoGorelli MarcoGorelli marked this pull request as ready for review March 12, 2025 16:20
to_replace: ReplaceValue | Mapping[Hashable, ReplaceValue] = ...,
value: ReplaceValue | Mapping[Hashable, ReplaceValue] = ...,
to_replace: ReplaceValue | Mapping[HashableT2, ReplaceValue] = ...,
value: ReplaceValue | Mapping[HashableT2, ReplaceValue] = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be HashableT3 since it might not be the same type as HashableT2.

Same here and throughout

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the change, but I think if someone writes df.replace({'a': 1}, {0: 9}) then that's likely a user error. Though there's likely some valid usage I can't think, so perhaps best to err on the side of not letting false positives through

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the change, but I think if someone writes df.replace({'a': 1}, {0: 9}) then that's likely a user error. Though there's likely some valid usage I can't think, so perhaps best to err on the side of not letting false positives through

Yes, I agree. While this is valid:

>>> import pandas as pd
>>> df = pd.DataFrame([["a", 1], ["b", 2]], columns=["ab", "num"])
>>> df
  ab  num
0  a    1
1  b    2
>>> df.replace({"a":"foo", 2: 20})
    ab  num
0  foo    1
1    b   20

it's not a great idea to be mixing types in one statement. With the stubs, I try to support "normal" usage of pandas. Otherwise, the stubs get too wide and don't catch common errors.

@MarcoGorelli MarcoGorelli marked this pull request as draft March 12, 2025 16:28
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. When green, change from draft and ping me and I'll approve and merge

@MarcoGorelli MarcoGorelli marked this pull request as ready for review March 12, 2025 17:17
@MarcoGorelli MarcoGorelli marked this pull request as draft March 12, 2025 17:17
@MarcoGorelli MarcoGorelli marked this pull request as ready for review March 12, 2025 17:27
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @MarcoGorelli

@Dr-Irv Dr-Irv merged commit 4b99ad8 into pandas-dev:main Mar 12, 2025
13 checks passed
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

Successfully merging this pull request may close these issues.

DataFrame.replace incompatible with type "dict[str, dict[int, int]]"
2 participants