Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ Indexing
- Fixed bug in :meth:`DataFrame.loc` where assigning an iterable to a single cell in an ``object`` dtype column incorrectly raised a ``ValueError`` (:issue:`57962`)
- Fixed bug in :meth:`DataFrame.loc` where assigning with duplicate column names and new columns corrupted unrelated columns (:issue:`58317`)
- Fixed segfault in :meth:`DataFrame.loc` when repeatedly adding new rows to an object-dtype-indexed :class:`DataFrame` (:issue:`21968`)
-

Missing
^^^^^^^
Expand Down Expand Up @@ -408,6 +407,7 @@ Reshaping
- Bug in :func:`merge` where merging on a :class:`MultiIndex` containing ``NaN`` values mapped ``NaN`` keys to the last level value instead of ``NaN`` (:issue:`64492`)
- Bug in :meth:`DataFrame.pivot_table` with ``margins=True`` raising ``TypeError`` when ``values`` has an :class:`ExtensionDtype` that cannot hold ``NA`` (e.g. :class:`IntervalDtype` with an integer subtype) and no ``columns`` were specified (:issue:`55484`)
- Bug in :meth:`Index.union` where the result could be unsorted when both inputs were monotonic increasing but disjoint, when ``sort`` was not ``False`` (:issue:`54646`)
- Fixed bug in :meth:`Series.sort_values` where ``ignore_index=True`` had no effect on an already-sorted Series (:issue:`65833`)
- In :func:`pivot_table`, when ``values`` is empty, the aggregation will be computed on a Series of all NA values (:issue:`46475`)
-

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4079,9 +4079,12 @@ def sort_values(
sorted_index = nargsort(values_to_sort, kind, bool(ascending), na_position)

if is_range_indexer(sorted_index, len(sorted_index)):
result = self if inplace else self.copy(deep=False)
if ignore_index:
result.index = default_index(len(sorted_index))
if inplace:
return self._update_inplace(self)
return self.copy(deep=False)
return None
return result
Comment thread
rahul-COD3 marked this conversation as resolved.

result = self._constructor(
self._values[sorted_index], index=self.index[sorted_index], copy=False
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/series/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ def test_sort_values_ignore_index(
tm.assert_series_equal(result_ser, expected)
tm.assert_series_equal(ser, Series(original_list))

def test_sort_values_ignore_index_on_already_sorted(self):
# GH 65833 - ignore_index had no effect on already sorted Series
ser = Series([1, 2, 3], index=[2, 3, 4])
expected = Series([1, 2, 3], index=[0, 1, 2])
result = ser.sort_values(ignore_index=True)
tm.assert_series_equal(result, expected)

ser2 = Series([1, 2, 3], index=[2, 3, 4])
ser2.sort_values(ignore_index=True, inplace=True)
tm.assert_series_equal(ser2, expected)

ser3 = Series([1, 2, 3], index=[2, 3, 4])
result3 = ser3.sort_values(ignore_index=False)
tm.assert_series_equal(result3, Series([1, 2, 3], index=[2, 3, 4]))

def test_mergesort_descending_stability(self):
# GH 28697
s = Series([1, 2, 1, 3], ["first", "b", "second", "c"])
Expand Down
Loading