Skip to content
Merged
Changes from all 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
7 changes: 6 additions & 1 deletion tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2550,11 +2550,16 @@ def test_types_rename() -> None:
# Apparently all of these calls are accepted by pandas
check(assert_type(df.rename(columns={None: "b"}), pd.DataFrame), pd.DataFrame)
check(assert_type(df.rename(columns={"": "b"}), pd.DataFrame), pd.DataFrame)
check(assert_type(df.rename(columns={(2, 1): "b"}), pd.DataFrame), pd.DataFrame)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you change the stub files and invalidate this usage?

Copy link
Member Author

Choose a reason for hiding this comment

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

That would actually break, the issue is at run time when you try to rename a dataframe with an Index when the mapper is designed for a multiindex. Since we do not have the capacity to differenciate 'dataframe with index' and 'dataframe with multiindex' you can't force that behavior in the stubs. Do you see what I mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

One possible solution would be to only allow rename(column=d), where d is a dictionary containing tuples as keys and values of equal length, so that columns={(2, 1): "b"} will be forbidden.

However I couldn't work out how to implement this. For now, I'll leave it as it is.

check(
assert_type(df.rename(columns=lambda s: s.upper()), pd.DataFrame), pd.DataFrame
)

df_multiindex = pd.DataFrame(columns=[("a", 1), ("a", 2)])
check(
assert_type(df_multiindex.rename(columns={(1, 2): ("b", "a")}), pd.DataFrame),
pd.DataFrame,
)


def test_types_rename_axis() -> None:
df = pd.DataFrame({"col_name": [1, 2, 3]})
Expand Down