Skip to content

Commit d289ef5

Browse files
authored
Fix Series.rename_axis and DataFrame.rename_axis typing (#1048)
* Add `hdf5` requirement to setup instructions * Fix `Series.rename_axis` and `DataFrame.rename_axis` typing * Fix comment * Use `check(assert_type(...))` framework in tests * Combine loops * Restrict `hdf5` instruction to macOS * Inline `check(assert_type(...))` calls * Check invalid usage of `columns`
1 parent 47fc9b6 commit d289ef5

File tree

5 files changed

+96
-13
lines changed

5 files changed

+96
-13
lines changed

docs/setup.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Set Up Environment
22

33
- Make sure you have `python >= 3.10` installed.
4+
- If using macOS, you may need to install `hdf5` (e.g., via `brew install hdf5`).
45
- Install poetry: `pip install 'poetry>=1.8'`
56
- Install the project dependencies: `poetry update`
67
- Enter the virtual environment: `poetry shell`

pandas-stubs/core/frame.pyi

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,40 +2070,44 @@ class DataFrame(NDFrame, OpsMixin):
20702070
limit: int | None = ...,
20712071
tolerance=...,
20722072
) -> DataFrame: ...
2073+
# Rename axis with `mapper`, `axis`, and `inplace=True`
20732074
@overload
20742075
def rename_axis(
20752076
self,
2076-
mapper=...,
2077+
mapper: Scalar | ListLike | None = ...,
2078+
*,
20772079
axis: Axis | None = ...,
20782080
copy: _bool = ...,
2079-
*,
20802081
inplace: Literal[True],
20812082
) -> None: ...
2083+
# Rename axis with `mapper`, `axis`, and `inplace=False`
20822084
@overload
20832085
def rename_axis(
20842086
self,
2085-
mapper=...,
2087+
mapper: Scalar | ListLike | None = ...,
2088+
*,
20862089
axis: Axis | None = ...,
20872090
copy: _bool = ...,
2088-
*,
20892091
inplace: Literal[False] = ...,
20902092
) -> DataFrame: ...
2093+
# Rename axis with `index` and/or `columns` and `inplace=True`
20912094
@overload
20922095
def rename_axis(
20932096
self,
2097+
*,
20942098
index: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
20952099
columns: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
20962100
copy: _bool = ...,
2097-
*,
20982101
inplace: Literal[True],
20992102
) -> None: ...
2103+
# Rename axis with `index` and/or `columns` and `inplace=False`
21002104
@overload
21012105
def rename_axis(
21022106
self,
2107+
*,
21032108
index: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
21042109
columns: _str | Sequence[_str] | dict[_str | int, _str] | Callable | None = ...,
21052110
copy: _bool = ...,
2106-
*,
21072111
inplace: Literal[False] = ...,
21082112
) -> DataFrame: ...
21092113
def rfloordiv(

pandas-stubs/core/series.pyi

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,24 +2076,41 @@ class Series(IndexOpsMixin[S1], NDFrame):
20762076
numeric_only: _bool = ...,
20772077
**kwargs,
20782078
) -> Scalar: ...
2079+
# Rename axis with `mapper`, `axis`, and `inplace=True`
20792080
@overload
20802081
def rename_axis(
20812082
self,
2082-
mapper: Scalar | ListLike = ...,
2083-
index: Scalar | ListLike | Callable | dict | None = ...,
2084-
columns: Scalar | ListLike | Callable | dict | None = ...,
2083+
mapper: Scalar | ListLike | None = ...,
2084+
*,
20852085
axis: AxisIndex | None = ...,
20862086
copy: _bool = ...,
2087+
inplace: Literal[True],
2088+
) -> None: ...
2089+
# Rename axis with `mapper`, `axis`, and `inplace=False`
2090+
@overload
2091+
def rename_axis(
2092+
self,
2093+
mapper: Scalar | ListLike | None = ...,
20872094
*,
2095+
axis: AxisIndex | None = ...,
2096+
copy: _bool = ...,
2097+
inplace: Literal[False] = ...,
2098+
) -> Self: ...
2099+
# Rename axis with `index` and `inplace=True`
2100+
@overload
2101+
def rename_axis(
2102+
self,
2103+
*,
2104+
index: Scalar | ListLike | Callable | dict | None = ...,
2105+
copy: _bool = ...,
20882106
inplace: Literal[True],
20892107
) -> None: ...
2108+
# Rename axis with `index` and `inplace=False`
20902109
@overload
20912110
def rename_axis(
20922111
self,
2093-
mapper: Scalar | ListLike = ...,
2112+
*,
20942113
index: Scalar | ListLike | Callable | dict | None = ...,
2095-
columns: Scalar | ListLike | Callable | dict | None = ...,
2096-
axis: AxisIndex | None = ...,
20972114
copy: _bool = ...,
20982115
inplace: Literal[False] = ...,
20992116
) -> Self: ...

tests/test_frame.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,48 @@ def test_types_rename() -> None:
19551955
df.rename(columns=lambda s: s.upper())
19561956

19571957

1958+
def test_types_rename_axis() -> None:
1959+
df = pd.DataFrame({"col_name": [1, 2, 3]})
1960+
df.index.name = "a"
1961+
df.columns.name = "b"
1962+
1963+
# Rename axes with `mapper` and `axis`
1964+
check(assert_type(df.rename_axis("A"), pd.DataFrame), pd.DataFrame)
1965+
check(assert_type(df.rename_axis(["A"]), pd.DataFrame), pd.DataFrame)
1966+
check(assert_type(df.rename_axis(None), pd.DataFrame), pd.DataFrame)
1967+
check(assert_type(df.rename_axis("B", axis=1), pd.DataFrame), pd.DataFrame)
1968+
check(assert_type(df.rename_axis(["B"], axis=1), pd.DataFrame), pd.DataFrame)
1969+
check(assert_type(df.rename_axis(None, axis=1), pd.DataFrame), pd.DataFrame)
1970+
1971+
# Rename axes with `index` and `columns`
1972+
check(
1973+
assert_type(df.rename_axis(index="A", columns="B"), pd.DataFrame),
1974+
pd.DataFrame,
1975+
)
1976+
check(
1977+
assert_type(df.rename_axis(index=["A"], columns=["B"]), pd.DataFrame),
1978+
pd.DataFrame,
1979+
)
1980+
check(
1981+
assert_type(df.rename_axis(index={"a": "A"}, columns={"b": "B"}), pd.DataFrame),
1982+
pd.DataFrame,
1983+
)
1984+
check(
1985+
assert_type(
1986+
df.rename_axis(
1987+
index=lambda name: name.upper(),
1988+
columns=lambda name: name.upper(),
1989+
),
1990+
pd.DataFrame,
1991+
),
1992+
pd.DataFrame,
1993+
)
1994+
check(
1995+
assert_type(df.rename_axis(index=None, columns=None), pd.DataFrame),
1996+
pd.DataFrame,
1997+
)
1998+
1999+
19582000
def test_types_eq() -> None:
19592001
df1 = pd.DataFrame([[1, 2], [8, 9]], columns=["A", "B"])
19602002
res1: pd.DataFrame = df1 == 1

tests/test_series.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,26 @@ def test_types_eq() -> None:
11211121

11221122

11231123
def test_types_rename_axis() -> None:
1124-
s: pd.Series = pd.Series([1, 2, 3]).rename_axis("A")
1124+
s = pd.Series([1, 2, 3])
1125+
s.index.name = "a"
1126+
1127+
# Rename index with `mapper`
1128+
check(assert_type(s.rename_axis("A"), "pd.Series[int]"), pd.Series)
1129+
check(assert_type(s.rename_axis(["A"]), "pd.Series[int]"), pd.Series)
1130+
check(assert_type(s.rename_axis(None), "pd.Series[int]"), pd.Series)
1131+
1132+
# Rename index with `index`
1133+
check(assert_type(s.rename_axis(index="A"), "pd.Series[int]"), pd.Series)
1134+
check(assert_type(s.rename_axis(index=["A"]), "pd.Series[int]"), pd.Series)
1135+
check(assert_type(s.rename_axis(index={"a": "A"}), "pd.Series[int]"), pd.Series)
1136+
check(
1137+
assert_type(s.rename_axis(index=lambda name: name.upper()), "pd.Series[int]"),
1138+
pd.Series,
1139+
)
1140+
check(assert_type(s.rename_axis(index=None), "pd.Series[int]"), pd.Series)
1141+
1142+
if TYPE_CHECKING_INVALID_USAGE:
1143+
s.rename_axis(columns="A") # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
11251144

11261145

11271146
def test_types_values() -> None:

0 commit comments

Comments
 (0)