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

GH1139 Series.rename inplace #1140

Merged
merged 9 commits into from
Mar 7, 2025
Merged

Conversation

loicdiridollou
Copy link
Contributor

@loicdiridollou loicdiridollou commented Mar 2, 2025

Added some test framework migration as this was how I originally found the issue with rename

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 4, 2025

@loicdiridollou let me know when you think you'll get to this. I'd like to do a new release soon, and want to know if I should wait for this change or not.

@loicdiridollou
Copy link
Contributor Author

loicdiridollou commented Mar 4, 2025 via email

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.

Pretty close. Will still wait to get this done before I release.

Comment on lines 1247 to 1250
# TODO this should not raise an error, the lambda is matched with Hashable
# check(
# assert_type(pd.Series([1, 2, 3]).rename(lambda x: x**2, inplace=True), None), type(None)
# )
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the solution here is to not use Renamer, and have the first overload be for Callable[[Any], Label] with Literal[True] returning None, then have the second overload be for Hashable | None returning Self and the third overload just having Mapping[Any, Label] as the argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I do so mypy seems to match the return of the expression with Any instead of None, let me see how I can address

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have pushed for you to take a look but not sure how to address, this is a mypy issue and not pyright, pyright is happy with the current setup and does not flag it.

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.

I got it to work. The issue is that Callable and Hashable are overlapping and mypy is having trouble with that. So instead of using Hashable, I used Scalar | tuple[Hashable, ...] which corresponds to the docs anyway.

So my review contains all the changes (as suggestions) that you should need to make.

@@ -1052,6 +1064,17 @@ class Series(IndexOpsMixin[S1], NDFrame):
errors: IgnoreRaise = ...,
) -> None: ...
@overload
def rename( # type: ignore[overload-overlap]
self,
index: Hashable | None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
index: Hashable | None,
index: Scalar | tuple[Hashable,...] | None,

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.

@Dr-Irv Dr-Irv merged commit 2938805 into pandas-dev:main Mar 7, 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.

Series().rename(..., inplace=True) can return a Series if the renamer is hashable
2 participants