Skip to content

Commit b6cfeae

Browse files
committed
Merge pull request pandas-dev#6639 from jreback/sort_error
API: A tuple passed to DataFame.sort_index will be interpreted as the levels of the index (GH4370)
2 parents 361f703 + 7bb9174 commit b6cfeae

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

doc/source/basics.rst

+12-3
Original file line numberDiff line numberDiff line change
@@ -1287,9 +1287,18 @@ Some other sorting notes / nuances:
12871287
* ``Series.sort`` sorts a Series by value in-place. This is to provide
12881288
compatibility with NumPy methods which expect the ``ndarray.sort``
12891289
behavior.
1290-
* ``DataFrame.sort`` takes a ``column`` argument instead of ``by``. This
1291-
method will likely be deprecated in a future release in favor of just using
1292-
``sort_index``.
1290+
1291+
Sorting by a multi-index column
1292+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1293+
1294+
You must be explicit about sorting when the column is a multi-index, and fully specify
1295+
all levels to ``by``.
1296+
1297+
.. ipython:: python
1298+
1299+
df1.columns = MultiIndex.from_tuples([('a','one'),('a','two'),('b','three')])
1300+
df1.sort_index(by=('a','two'))
1301+
12931302
12941303
Copying
12951304
-------

doc/source/release.rst

+11
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ API Changes
133133

134134
- Allow specification of a more complex groupby, via ``pd.Groupby`` (:issue:`3794`)
135135

136+
- A tuple passed to ``DataFame.sort_index`` will be interpreted as the levels of
137+
the index, rather than requiring a list of tuple (:issue:`4370`)
138+
139+
Deprecations
140+
~~~~~~~~~~~~
141+
142+
Prior Version Deprecations/Changes
143+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144+
145+
- Remove ``column`` keyword from ``DataFrame.sort`` (:issue:`4370`)
146+
136147
Experimental Features
137148
~~~~~~~~~~~~~~~~~~~~~
138149

doc/source/v0.14.0.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ You can use a right-hand-side of an alignable object as well.
288288
Prior Version Deprecations/Changes
289289
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290290

291-
There are no announced changes in 0.13.1 or prior that are taking effect as of 0.14.0
291+
Therse are prior version deprecations that are taking effect as of 0.14.0.
292+
293+
- Remove ``column`` keyword from ``DataFrame.sort`` (:issue:`4370`)
292294

293295
Deprecations
294296
~~~~~~~~~~~~

pandas/core/frame.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ def _m8_to_i8(x):
25302530
#----------------------------------------------------------------------
25312531
# Sorting
25322532

2533-
def sort(self, columns=None, column=None, axis=0, ascending=True,
2533+
def sort(self, columns=None, axis=0, ascending=True,
25342534
inplace=False):
25352535
"""
25362536
Sort DataFrame either by labels (along either axis) or by the values in
@@ -2539,8 +2539,9 @@ def sort(self, columns=None, column=None, axis=0, ascending=True,
25392539
Parameters
25402540
----------
25412541
columns : object
2542-
Column name(s) in frame. Accepts a column name or a list or tuple
2543-
for a nested sort.
2542+
Column name(s) in frame. Accepts a column name or a list
2543+
for a nested sort. A tuple will be interpreted as the
2544+
levels of a multi-index.
25442545
ascending : boolean or list, default True
25452546
Sort ascending vs. descending. Specify list for multiple sort
25462547
orders
@@ -2557,9 +2558,6 @@ def sort(self, columns=None, column=None, axis=0, ascending=True,
25572558
-------
25582559
sorted : DataFrame
25592560
"""
2560-
if column is not None: # pragma: no cover
2561-
warnings.warn("column is deprecated, use columns", FutureWarning)
2562-
columns = column
25632561
return self.sort_index(by=columns, axis=axis, ascending=ascending,
25642562
inplace=inplace)
25652563

@@ -2574,8 +2572,9 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
25742572
axis : {0, 1}
25752573
Sort index/rows versus columns
25762574
by : object
2577-
Column name(s) in frame. Accepts a column name or a list or tuple
2578-
for a nested sort.
2575+
Column name(s) in frame. Accepts a column name or a list
2576+
for a nested sort. A tuple will be interpreted as the
2577+
levels of a multi-index.
25792578
ascending : boolean or list, default True
25802579
Sort ascending vs. descending. Specify list for multiple sort
25812580
orders
@@ -2602,7 +2601,7 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
26022601
if axis != 0:
26032602
raise ValueError('When sorting by column, axis must be 0 '
26042603
'(rows)')
2605-
if not isinstance(by, (tuple, list)):
2604+
if not isinstance(by, list):
26062605
by = [by]
26072606
if com._is_sequence(ascending) and len(by) != len(ascending):
26082607
raise ValueError('Length of ascending (%d) != length of by'
@@ -2629,6 +2628,13 @@ def trans(v):
26292628
by = by[0]
26302629
k = self[by].values
26312630
if k.ndim == 2:
2631+
2632+
# try to be helpful
2633+
if isinstance(self.columns, MultiIndex):
2634+
raise ValueError('Cannot sort by column %s in a multi-index'
2635+
' you need to explicity provide all the levels'
2636+
% str(by))
2637+
26322638
raise ValueError('Cannot sort by duplicate column %s'
26332639
% str(by))
26342640
if isinstance(ascending, (tuple, list)):

pandas/tests/test_frame.py

+11
Original file line numberDiff line numberDiff line change
@@ -9797,6 +9797,17 @@ def test_sort_index_duplicates(self):
97979797
# multi-column 'by' is separate codepath
97989798
df.sort_index(by=['a', 'b'])
97999799

9800+
# with multi-index
9801+
# GH4370
9802+
df = DataFrame(np.random.randn(4,2),columns=MultiIndex.from_tuples([('a',0),('a',1)]))
9803+
with assertRaisesRegexp(ValueError, 'levels'):
9804+
df.sort_index(by='a')
9805+
9806+
# convert tuples to a list of tuples
9807+
expected = df.sort_index(by=[('a',1)])
9808+
result = df.sort_index(by=('a',1))
9809+
assert_frame_equal(result, expected)
9810+
98009811
def test_sort_datetimes(self):
98019812

98029813
# GH 3461, argsort / lexsort differences for a datetime column

0 commit comments

Comments
 (0)