Skip to content

Commit 279f597

Browse files
authored
BUG: Fix sorting by column named None in DataFrame.sort_values (GH#61512) (#61517)
BUG: Allow sorting by column named None in DataFrame.sort_values (GH#61512)
1 parent 9069247 commit 279f597

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ Other
891891
- Bug in :meth:`DataFrame.query` which raised an exception when querying integer column names using backticks. (:issue:`60494`)
892892
- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`)
893893
- 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`)
894+
- 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`)
894895
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
895896
- 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`)
896897
- 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/generic.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,11 +1645,7 @@ def _is_label_reference(self, key: Level, axis: Axis = 0) -> bool:
16451645
axis_int = self._get_axis_number(axis)
16461646
other_axes = (ax for ax in range(self._AXIS_LEN) if ax != axis_int)
16471647

1648-
return (
1649-
key is not None
1650-
and is_hashable(key)
1651-
and any(key in self.axes[ax] for ax in other_axes)
1652-
)
1648+
return is_hashable(key) and any(key in self.axes[ax] for ax in other_axes)
16531649

16541650
@final
16551651
def _is_label_or_level_reference(self, key: Level, axis: AxisInt = 0) -> bool:

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)