Skip to content

Conversation

@loicdiridollou
Copy link
Member

  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

PR replaces the DataFrame.rename when using a multi index in the replacer while the dataframe only has Index.

@loicdiridollou
Copy link
Member Author

/pandas_nightly

@loicdiridollou
Copy link
Member Author

# 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.

Copy link
Contributor

@cmp0xff cmp0xff left a comment

Choose a reason for hiding this comment

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

Thank you @loicdiridollou !

# 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.

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.

@cmp0xff cmp0xff merged commit 72138f7 into pandas-dev:main Oct 25, 2025
14 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.

2 participants