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

Series.rename_axis(None) not accepted #1042

Open
dpoznik opened this issue Nov 21, 2024 · 4 comments · May be fixed by #1048
Open

Series.rename_axis(None) not accepted #1042

dpoznik opened this issue Nov 21, 2024 · 4 comments · May be fixed by #1048
Assignees
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure

Comments

@dpoznik
Copy link

dpoznik commented Nov 21, 2024

Describe the bug
To remove the name of the index of a Series, one may call Series.rename_axis(None), however mypy does not recognize None as a valid argument.

To Reproduce
This code:

import pandas as pd

s1 = pd.Series([1, 2, 3], name="values").rename_axis("index")
s2 = s1.rename_axis(None)
print(f"s1\n{s1}\n\ns2\n{s2}\n\n")

elicits the expected output when run:

s1
index
0    1
1    2
2    3
Name: values, dtype: int64

s2
0    1
1    2
2    3
Name: values, dtype: int64

but mypy complains with:

foo.py:4: error: No overload variant of "rename_axis" of "Series" matches argument type "None"  [call-overload]
foo.py:4: note: Possible overload variants:
foo.py:4: note:     def [ListLike: (Sequence[Any], ndarray[Any, Any], Series[Any], Index[Any])] rename_axis(self, mapper: str | bytes | date | datetime | timedelta | <9 more items> = ..., index: str | bytes | date | datetime | timedelta | <11 more items> | None = ..., columns: str | bytes | date | datetime | timedelta | <11 more items> | None = ..., axis: Literal['index', 0] | None = ..., copy: bool = ..., *, inplace: Literal[True]) -> None
foo.py:4: note:     def [ListLike: (Sequence[Any], ndarray[Any, Any], Series[Any], Index[Any])] rename_axis(self, mapper: str | bytes | date | datetime | timedelta | <9 more items> = ..., index: str | bytes | date | datetime | timedelta | <11 more items> | None = ..., columns: str | bytes | date | datetime | timedelta | <11 more items> | None = ..., axis: Literal['index', 0] | None = ..., copy: bool = ..., inplace: Literal[False] = ...) -> Series[int]
Found 1 error in 1 file (checked 1 source file)

Please complete the following information:

  • OS: macOS 14.7.1
  • Python version: 3.12.7
  • Mypy version: 1.13.0
  • Pandas-stubs version: 2.2.3.241009
  • Pandas version: 2.2.3
@loicdiridollou
Copy link
Contributor

Hey @dpoznik ,

Thanks for the report! What you are seeing is correct, there exists no overload for passing None (although it is allowed by the pandas API https://pandas.pydata.org/docs/reference/api/pandas.Series.rename_axis.html#pandas.Series.rename_axis).
We should add an overload to pass None as mapper. Do you confirm @Dr-Irv ?

@overload
def rename_axis(
self,
mapper: Scalar | ListLike = ...,
index: Scalar | ListLike | Callable | dict | None = ...,
columns: Scalar | ListLike | Callable | dict | None = ...,
axis: AxisIndex | None = ...,
copy: _bool = ...,
*,
inplace: Literal[True],
) -> None: ...
@overload
def rename_axis(
self,
mapper: Scalar | ListLike = ...,
index: Scalar | ListLike | Callable | dict | None = ...,
columns: Scalar | ListLike | Callable | dict | None = ...,
axis: AxisIndex | None = ...,
copy: _bool = ...,
inplace: Literal[False] = ...,
) -> Self: ...

@loicdiridollou loicdiridollou added Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure labels Nov 21, 2024
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 21, 2024

We should add an overload to pass None as mapper. Do you confirm @Dr-Irv ?

I don't think an overload is needed. Just need to add None as a possible argument to mapper .

I also think the asterisk needs to be moved to after mapper . In addition, for Series.rename_axis(), the columns argument should not be allowed and should be removed.

A change to allow None should also be made in frame.pyi to set the type of the mapper argument there.

PR with tests welcome.

@dpoznik
Copy link
Author

dpoznik commented Nov 21, 2024

Cool, thanks for confirming. I'd be happy to submit a PR with all of the above at some point in the next few days. Could you assign the issue to me to ensure against duplicated effort?

@loicdiridollou
Copy link
Contributor

You got it @dpoznik ! Looking forward!

@dpoznik dpoznik linked a pull request Nov 21, 2024 that will close this issue
2 tasks
@dpoznik dpoznik changed the title Series.rename_axis(None) not accepted Series.rename_axis(None) not accepted Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants