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 Series.rename_axis and DataFrame.rename_axis typing #1048

Merged
merged 8 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Set Up Environment

- Make sure you have `python >= 3.10` installed.
- Make sure you have `hdf5` installed.
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
- Install poetry: `pip install 'poetry>=1.8'`
- Install the project dependencies: `poetry update`
- Enter the virtual environment: `poetry shell`
Expand Down
16 changes: 10 additions & 6 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2070,40 +2070,44 @@ class DataFrame(NDFrame, OpsMixin):
limit: int | None = ...,
tolerance=...,
) -> DataFrame: ...
# Rename axis with `mapper`, `axis`, and `inplace=True`
@overload
def rename_axis(
self,
mapper=...,
mapper: Scalar | ListLike | None = ...,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
*,
axis: Axis | None = ...,
copy: _bool = ...,
*,
inplace: Literal[True],
) -> None: ...
# Rename axis with `mapper`, `axis`, and `inplace=False`
@overload
def rename_axis(
self,
mapper=...,
mapper: Scalar | ListLike | None = ...,
*,
axis: Axis | None = ...,
copy: _bool = ...,
*,
inplace: Literal[False] = ...,
) -> DataFrame: ...
# Rename axis with `index` and/or `columns` and `inplace=True`
@overload
def rename_axis(
self,
*,
index: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
columns: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
copy: _bool = ...,
*,
inplace: Literal[True],
) -> None: ...
# Rename axis with `index` and/or `columns` and `inplace=False`
@overload
def rename_axis(
self,
*,
index: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
columns: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
copy: _bool = ...,
*,
inplace: Literal[False] = ...,
) -> DataFrame: ...
def rfloordiv(
Expand Down
29 changes: 23 additions & 6 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2076,24 +2076,41 @@ class Series(IndexOpsMixin[S1], NDFrame):
numeric_only: _bool = ...,
**kwargs,
) -> Scalar: ...
# Rename axis with `mapper`, `axis`, and `inplace=True`
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
@overload
def rename_axis(
self,
mapper: Scalar | ListLike = ...,
index: Scalar | ListLike | Callable | dict | None = ...,
columns: Scalar | ListLike | Callable | dict | None = ...,
mapper: Scalar | ListLike | None = ...,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
*,
axis: AxisIndex | None = ...,
copy: _bool = ...,
inplace: Literal[True],
) -> None: ...
# Rename axis with `mapper`, `axis`, and `inplace=False`
@overload
def rename_axis(
self,
mapper: Scalar | ListLike | None = ...,
*,
axis: AxisIndex | None = ...,
copy: _bool = ...,
inplace: Literal[False] = ...,
) -> Self: ...
# Rename axis with `index` and `inplace=True`
@overload
def rename_axis(
self,
*,
index: Scalar | ListLike | Callable | dict | None = ...,
copy: _bool = ...,
inplace: Literal[True],
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
) -> None: ...
# Rename axis with `index` and `inplace=False`
@overload
def rename_axis(
self,
mapper: Scalar | ListLike = ...,
*,
index: Scalar | ListLike | Callable | dict | None = ...,
columns: Scalar | ListLike | Callable | dict | None = ...,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
axis: AxisIndex | None = ...,
copy: _bool = ...,
inplace: Literal[False] = ...,
) -> Self: ...
Expand Down
26 changes: 26 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,32 @@ def test_types_rename() -> None:
df.rename(columns=lambda s: s.upper())


def test_types_rename_axis() -> None:
df = pd.DataFrame({"col_name": [1, 2, 3]})
df.index.name = "a"
df.columns.name = "b"

for _df in [
# Rename axes with `mapper` and `axis`
df.rename_axis("A"),
df.rename_axis(["A"]),
df.rename_axis(None),
df.rename_axis("B", axis=1),
df.rename_axis(["B"], axis=1),
df.rename_axis(None, axis=1),
# Rename axes with `index` and `columns`
df.rename_axis(index="A", columns="B"),
df.rename_axis(index=["A"], columns=["B"]),
df.rename_axis(index={"a": "A"}, columns={"b": "B"}),
df.rename_axis(
index=lambda name: name.upper(),
columns=lambda name: name.upper(),
),
df.rename_axis(index=None, columns=None),
]:
check(assert_type(_df, pd.DataFrame), pd.DataFrame)
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved


def test_types_eq() -> None:
df1 = pd.DataFrame([[1, 2], [8, 9]], columns=["A", "B"])
res1: pd.DataFrame = df1 == 1
Expand Down
17 changes: 16 additions & 1 deletion tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,22 @@ def test_types_eq() -> None:


def test_types_rename_axis() -> None:
s: pd.Series = pd.Series([1, 2, 3]).rename_axis("A")
s = pd.Series([1, 2, 3])
s.index.name = "a"

for _s in [
# Rename index with `mapper`
s.rename_axis("A"),
s.rename_axis(["A"]),
s.rename_axis(None),
# Rename index with `index`
s.rename_axis(index="A"),
s.rename_axis(index=["A"]),
s.rename_axis(index={"a": "A"}),
s.rename_axis(index=lambda name: name.upper()),
s.rename_axis(index=None),
]:
check(assert_type(_s, pd.Series[int]), pd.Series[int])
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved


def test_types_values() -> None:
Expand Down