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

DOC: Updated set_index doc with a warning #60990

Merged
merged 6 commits into from
Mar 5, 2025

Conversation

SaraInCode
Copy link
Contributor

Sorry, something went wrong.

@@ -5868,6 +5868,10 @@ def set_index(
columns or arrays (of the correct length). The index can replace the
existing index or expand on it.
.. warning::
Copy link
Member

Choose a reason for hiding this comment

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

Instead, could you show this as an example in the Examples section instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll probably be removing the warning but I could show an example

Copy link
Member

Choose a reason for hiding this comment

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

@mroeschke - good here?

@mroeschke mroeschke added the Docs label Feb 25, 2025
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Comment on lines 5872 to 5873
Setting a new index will remove the current index column,
unless you first call `reset_index`.
Copy link
Member

Choose a reason for hiding this comment

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

This is only true when append=False. As mentioned in the issue, this seems to me to be redundant with the line above:

The index can replace the existing index or expand on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. Though it seems redundant with the line you mentioned. When you look at,

append: bool, default False

Whether to append columns to existing index.

it doesn't directly explain the behavior of replacing the existing index (by default). Does this statement makes it clear?

append: bool, default False

Whether to append new columns to the existing index . If set to True, the current index will remain unchanged and new columns will be added.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed we should add something here, but saying it is unchanged and columns are added seems inconsistent to me. I also think the True behavior is already clear by Whether to append new columns to the existing index. However the False behavior could perhaps use elaboration. I would suggest:

When False, the current index will be dropped from the DataFrame.

Copy link
Member

Choose a reason for hiding this comment

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

But not strongly opposed to adding elaboration for the True case either.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Looks good! One request.

Append a column to the existing index:
>>> df.set_index("month", inplace=True)
Copy link
Member

Choose a reason for hiding this comment

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

We will be deprecating inplace in the near-ish future. Can you do df = df.set_index(...) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure do!!

Append a column to the existing index:
>>> a = df.set_index("month")
Copy link
Member

Choose a reason for hiding this comment

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

I had suggested df. I would stay away from a as it has no particular meeting. Is there a reason to not use df here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought not to disturb the original dataframe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nevertheless, I've updated to df.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm

@mroeschke mroeschke added this to the 3.0 milestone Mar 5, 2025
@mroeschke mroeschke merged commit 56847c5 into pandas-dev:main Mar 5, 2025
42 checks passed
@mroeschke
Copy link
Member

Thanks @SaraInCode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: No warning in set_index() that previous index column is removed.
3 participants