Skip to content

BUG: Series.sort_values ignore_index has no effect on already sorted Series (#65833)#65836

Open
rahul-COD3 wants to merge 3 commits into
pandas-dev:mainfrom
rahul-COD3:main
Open

BUG: Series.sort_values ignore_index has no effect on already sorted Series (#65833)#65836
rahul-COD3 wants to merge 3 commits into
pandas-dev:mainfrom
rahul-COD3:main

Conversation

@rahul-COD3

Copy link
Copy Markdown

Problem: When a Series is already sorted, sort_values() returns early without applying ignore_index.

Root cause: The early return path (for the already-sorted case) at pandas/core/series.py:4081 copied the Series directly without checking ignore_index.

Fix: Move the ignore_index index reset into the early return path for both the inplace and non-inplace branches.

Closes #65833

@rhshadrach rhshadrach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR. Any behavior change (including bugfixes) need tests and an entry in the whatsnew; this should go in v3.1.0.

@rahul-COD3

Copy link
Copy Markdown
Author

Added a test for the already-sorted case (test_sort_values_ignore_index_on_already_sorted) and a whatsnew entry for v3.1.0 under the Indexing section. All 13 tests pass.

@rahul-COD3 rahul-COD3 force-pushed the main branch 3 times, most recently from 3a60bd1 to cb55245 Compare June 10, 2026 16:12
@rahul-COD3 rahul-COD3 requested a review from rhshadrach June 10, 2026 16:41

@rhshadrach rhshadrach left a comment

Copy link
Copy Markdown
Member

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 thread pandas/core/series.py
Comment thread doc/source/whatsnew/v3.1.0.rst Outdated
…as-dev#65833)

When a Series is already sorted, sort_values() takes an early return
path that bypasses the ignore_index reset. Move the ignore_index check
into the early return path so it is applied in all cases.
…as-dev#65833)

Refactor early return in sort_values to be more DRY per review feedback.
Move whatsnew entry from Indexing to Reshaping section.
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.

BUG: ignore_index=True has no effect on already sorted Series

2 participants