Skip to content

Commit 488423b

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

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7188,7 +7188,7 @@ def sort_values(
71887188
if key is not None:
71897189
# error: Incompatible types in assignment (expression has type
71907190
# "Series", variable has type "ndarray")
7191-
k = Series(k, name=by[0]) # type: ignore[assignment]
7191+
k = Series(k, name=by[0])
71927192

71937193
if isinstance(ascending, (tuple, list)):
71947194
ascending = ascending[0]

pandas/core/generic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,9 @@ def _get_label_or_level_values(self, key: Level, axis: AxisInt = 0) -> ArrayLike
17571757
(ax for ax in range(self._AXIS_LEN) if ax != axis), None
17581758
)
17591759

1760-
if self._is_label_reference(key, axis=axis):
1760+
if axis == 0 and key in self.columns:
1761+
values = self[key]._values
1762+
elif self._is_label_reference(key, axis=axis):
17611763
self._check_label_or_level_ambiguity(key, axis=axis)
17621764
if first_other_axes is None:
17631765
raise ValueError("axis matched all axes")

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)