Skip to content

Commit 39aecc7

Browse files
committed
BUG: Allow sorting by column named None in DataFrame.sort_values (GH#61512)
1 parent cfe54bd commit 39aecc7

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ Other
886886
- Bug in :meth:`DataFrame.query` which raised an exception when querying integer column names using backticks. (:issue:`60494`)
887887
- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`)
888888
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
889+
- Bug in :meth:`DataFrame.sort_values` where sorting by a column explicitly named ``None`` raised a ``KeyError`` instead of sorting by the column as expected. (:issue:`61512`)
889890
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
890891
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
891892
- Bug in :meth:`Index.sort_values` when passing a key function that turns values into tuples, e.g. ``key=natsort.natsort_key``, would raise ``TypeError`` (:issue:`56081`)

pandas/core/frame.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7181,14 +7181,17 @@ def sort_values(
71817181
)
71827182
elif len(by):
71837183
# len(by) == 1
7184-
7185-
k = self._get_label_or_level_values(by[0], axis=axis)
7184+
7185+
if by[0] is None and None in self.columns and axis == 0:
7186+
k = self[None]._values
7187+
else:
7188+
k = self._get_label_or_level_values(by[0], axis=axis)
71867189

71877190
# need to rewrap column in Series to apply key function
71887191
if key is not None:
71897192
# error: Incompatible types in assignment (expression has type
71907193
# "Series", variable has type "ndarray")
7191-
k = Series(k, name=by[0]) # type: ignore[assignment]
7194+
k = Series(k, name=by[0])
71927195

71937196
if isinstance(ascending, (tuple, list)):
71947197
ascending = ascending[0]

pandas/tests/frame/methods/test_sort_values.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,13 @@ def test_sort_values_no_op_reset_index(self):
630630
expected = DataFrame({"A": [10, 20], "B": [1, 5]})
631631
tm.assert_frame_equal(result, expected)
632632

633+
def test_sort_by_column_named_none(self):
634+
# GH#61512
635+
df = DataFrame([[3, 1], [2, 2]], columns=[None, "C1"])
636+
result = df.sort_values(by=None)
637+
expected = DataFrame([[2, 2], [3, 1]], columns=[None, "C1"], index=[1, 0])
638+
tm.assert_frame_equal(result, expected)
639+
633640

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

0 commit comments

Comments
 (0)