Skip to content

Commit 34191dd

Browse files
authored
BUG: Fix sort_values bug that creates unprintable object (#39464)
1 parent 8dc4341 commit 34191dd

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ Reshaping
414414
- Bug in :meth:`DataFrame.join` not assigning values correctly when having :class:`MultiIndex` where at least one dimension is from dtype ``Categorical`` with non-alphabetically sorted categories (:issue:`38502`)
415415
- :meth:`Series.value_counts` and :meth:`Series.mode` return consistent keys in original order (:issue:`12679`, :issue:`11227` and :issue:`39007`)
416416
- Bug in :meth:`DataFrame.apply` would give incorrect results when used with a string argument and ``axis=1`` when the axis argument was not supported and now raises a ``ValueError`` instead (:issue:`39211`)
417+
- Bug in :meth:`DataFrame.sort_values` not reshaping index correctly after sorting on columns, when ``ignore_index=True`` (:issue:`39464`)
417418
- Bug in :meth:`DataFrame.append` returning incorrect dtypes with combinations of ``ExtensionDtype`` dtypes (:issue:`39454`)
418419

419420
Sparse

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5560,7 +5560,9 @@ def sort_values( # type: ignore[override]
55605560
)
55615561

55625562
if ignore_index:
5563-
new_data.set_axis(1, ibase.default_index(len(indexer)))
5563+
new_data.set_axis(
5564+
self._get_block_manager_axis(axis), ibase.default_index(len(indexer))
5565+
)
55645566

55655567
result = self._constructor(new_data)
55665568
if inplace:

pandas/tests/frame/methods/test_sort_values.py

+8
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,14 @@ def test_sort_values_item_cache(self, using_array_manager):
579579

580580
assert df.iloc[0, 0] == df["A"][0]
581581

582+
def test_sort_values_reshaping(self):
583+
# GH 39426
584+
values = list(range(21))
585+
expected = DataFrame([values], columns=values)
586+
df = expected.sort_values(expected.index[0], axis=1, ignore_index=True)
587+
588+
tm.assert_frame_equal(df, expected)
589+
582590

583591
class TestDataFrameSortKey: # test key sorting (issue 27237)
584592
def test_sort_values_inplace_key(self, sort_by_key):

0 commit comments

Comments
 (0)