Skip to content

Commit 8a7d9fc

Browse files
committed
Merge pull request #10937 from glyg/master
updating docs for the new sorting mechanisms - GH #10886
2 parents 40b17da + 586c77e commit 8a7d9fc

File tree

10 files changed

+35
-32
lines changed

10 files changed

+35
-32
lines changed

doc/source/10min.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Sorting by values
157157

158158
.. ipython:: python
159159
160-
df.sort(columns='B')
160+
df.sort_values(by='B')
161161
162162
Selection
163163
---------
@@ -680,7 +680,7 @@ Sorting is per order in the categories, not lexical order.
680680

681681
.. ipython:: python
682682
683-
df.sort("grade")
683+
df.sort_values(by="grade")
684684
685685
Grouping by a categorical column shows also empty categories.
686686

doc/source/advanced.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ As usual, **both sides** of the slicers are included as this is label indexing.
286286
names=['lvl0', 'lvl1'])
287287
dfmi = pd.DataFrame(np.arange(len(miindex)*len(micolumns)).reshape((len(miindex),len(micolumns))),
288288
index=miindex,
289-
columns=micolumns).sortlevel().sortlevel(axis=1)
289+
columns=micolumns).sort_index().sort_index(axis=1)
290290
dfmi
291291
292292
Basic multi-index slicing using slices, lists, and labels.
@@ -458,7 +458,7 @@ correctly. You can think about breaking the axis into unique groups, where at
458458
the hierarchical level of interest, each distinct group shares a label, but no
459459
two have the same label. However, the ``MultiIndex`` does not enforce this:
460460
**you are responsible for ensuring that things are properly sorted**. There is
461-
an important new method ``sortlevel`` to sort an axis within a ``MultiIndex``
461+
an important new method ``sort_index`` to sort an axis within a ``MultiIndex``
462462
so that its labels are grouped and sorted by the original ordering of the
463463
associated factor at that level. Note that this does not necessarily mean the
464464
labels will be sorted lexicographically!
@@ -468,34 +468,34 @@ labels will be sorted lexicographically!
468468
import random; random.shuffle(tuples)
469469
s = pd.Series(np.random.randn(8), index=pd.MultiIndex.from_tuples(tuples))
470470
s
471-
s.sortlevel(0)
472-
s.sortlevel(1)
471+
s.sort_index(level=0)
472+
s.sort_index(level=1)
473473
474474
.. _advanced.sortlevel_byname:
475475

476-
Note, you may also pass a level name to ``sortlevel`` if the MultiIndex levels
476+
Note, you may also pass a level name to ``sort_index`` if the MultiIndex levels
477477
are named.
478478

479479
.. ipython:: python
480480
481481
s.index.set_names(['L1', 'L2'], inplace=True)
482-
s.sortlevel(level='L1')
483-
s.sortlevel(level='L2')
482+
s.sort_index(level='L1')
483+
s.sort_index(level='L2')
484484
485485
Some indexing will work even if the data are not sorted, but will be rather
486486
inefficient and will also return a copy of the data rather than a view:
487487

488488
.. ipython:: python
489489
490490
s['qux']
491-
s.sortlevel(1)['qux']
491+
s.sort_index(level=1)['qux']
492492
493493
On higher dimensional objects, you can sort any of the other axes by level if
494494
they have a MultiIndex:
495495

496496
.. ipython:: python
497497
498-
df.T.sortlevel(1, axis=1)
498+
df.T.sort_index(level=1, axis=1)
499499
500500
The ``MultiIndex`` object has code to **explicity check the sort depth**. Thus,
501501
if you try to index at a depth at which the index is not sorted, it will raise

doc/source/basics.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ equality to be True:
328328
df1 = pd.DataFrame({'col':['foo', 0, np.nan]})
329329
df2 = pd.DataFrame({'col':[np.nan, 0, 'foo']}, index=[2,1,0])
330330
df1.equals(df2)
331-
df1.equals(df2.sort())
331+
df1.equals(df2.sort_index())
332332
333333
Comparing array-like objects
334334
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1489,16 +1489,16 @@ The ``by`` argument can take a list of column names, e.g.:
14891489

14901490
.. ipython:: python
14911491
1492-
df1[['one', 'two', 'three']].sort_index(by=['one','two'])
1492+
df1[['one', 'two', 'three']].sort_values(by=['one','two'])
14931493
14941494
These methods have special treatment of NA values via the ``na_position``
14951495
argument:
14961496

14971497
.. ipython:: python
14981498
14991499
s[2] = np.nan
1500-
s.order()
1501-
s.order(na_position='first')
1500+
s.sort_values()
1501+
s.sort_values(na_position='first')
15021502
15031503
15041504
.. _basics.searchsorted:
@@ -1564,7 +1564,7 @@ all levels to ``by``.
15641564
.. ipython:: python
15651565
15661566
df1.columns = pd.MultiIndex.from_tuples([('a','one'),('a','two'),('b','three')])
1567-
df1.sort_index(by=('a','two'))
1567+
df1.sort_values(by=('a','two'))
15681568
15691569
15701570
Copying

doc/source/categorical.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ meaning and certain operations are possible. If the categorical is unordered, ``
280280
.. ipython:: python
281281
282282
s = pd.Series(pd.Categorical(["a","b","c","a"], ordered=False))
283-
s.sort()
283+
s.sort_values(inplace=True)
284284
s = pd.Series(["a","b","c","a"]).astype('category', ordered=True)
285-
s.sort()
285+
s.sort_values(inplace=True)
286286
s
287287
s.min(), s.max()
288288
@@ -302,7 +302,7 @@ This is even true for strings and numeric data:
302302
s = pd.Series([1,2,3,1], dtype="category")
303303
s = s.cat.set_categories([2,3,1], ordered=True)
304304
s
305-
s.sort()
305+
s.sort_values(inplace=True)
306306
s
307307
s.min(), s.max()
308308
@@ -320,7 +320,7 @@ necessarily make the sort order the same as the categories order.
320320
s = pd.Series([1,2,3,1], dtype="category")
321321
s = s.cat.reorder_categories([2,3,1], ordered=True)
322322
s
323-
s.sort()
323+
s.sort_values(inplace=True)
324324
s
325325
s.min(), s.max()
326326
@@ -349,14 +349,14 @@ The ordering of the categorical is determined by the ``categories`` of that colu
349349
350350
dfs = pd.DataFrame({'A' : pd.Categorical(list('bbeebbaa'), categories=['e','a','b'], ordered=True),
351351
'B' : [1,2,1,2,2,1,2,1] })
352-
dfs.sort(['A', 'B'])
352+
dfs.sort_values(by=['A', 'B'])
353353
354354
Reordering the ``categories`` changes a future sort.
355355

356356
.. ipython:: python
357357
358358
dfs['A'] = dfs['A'].cat.reorder_categories(['a','b','e'])
359-
dfs.sort(['A','B'])
359+
dfs.sort_values(by=['A','B'])
360360
361361
Comparisons
362362
-----------

doc/source/cookbook.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Method 2 : sort then take first of each
309309

310310
.. ipython:: python
311311
312-
df.sort("BBB").groupby("AAA", as_index=False).first()
312+
df.sort_values(by="BBB").groupby("AAA", as_index=False).first()
313313
314314
Notice the same results, with the exception of the index.
315315

@@ -410,7 +410,7 @@ Sorting
410410

411411
.. ipython:: python
412412
413-
df.sort(('Labs', 'II'), ascending=False)
413+
df.sort_values(by=('Labs', 'II'), ascending=False)
414414
415415
`Partial Selection, the need for sortedness;
416416
<https://github.com/pydata/pandas/issues/2995>`__
@@ -547,7 +547,7 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
547547
548548
code_groups = df.groupby('code')
549549
550-
agg_n_sort_order = code_groups[['data']].transform(sum).sort('data')
550+
agg_n_sort_order = code_groups[['data']].transform(sum).sort_values(by='data')
551551
552552
sorted_df = df.ix[agg_n_sort_order.index]
553553

doc/source/reshaping.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ will result in a **sorted** copy of the original DataFrame or Series:
164164
index = pd.MultiIndex.from_product([[2,1], ['a', 'b']])
165165
df = pd.DataFrame(np.random.randn(4), index=index, columns=['A'])
166166
df
167-
all(df.unstack().stack() == df.sort())
167+
all(df.unstack().stack() == df.sort_index())
168168
169-
while the above code will raise a ``TypeError`` if the call to ``sort`` is
169+
while the above code will raise a ``TypeError`` if the call to ``sort_index`` is
170170
removed.
171171

172172
.. _reshaping.stack_multiple:
@@ -206,7 +206,7 @@ Missing Data
206206
These functions are intelligent about handling missing data and do not expect
207207
each subgroup within the hierarchical index to have the same set of labels.
208208
They also can handle the index being unsorted (but you can make it sorted by
209-
calling ``sortlevel``, of course). Here is a more complex example:
209+
calling ``sort_index``, of course). Here is a more complex example:
210210

211211
.. ipython:: python
212212

doc/source/whatsnew/v0.13.1.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ API changes
120120
equal. (:issue:`5283`) See also :ref:`the docs<basics.equals>` for a motivating example.
121121

122122
.. ipython:: python
123-
123+
:okwarning:
124+
124125
df = DataFrame({'col':['foo', 0, np.nan]})
125126
df2 = DataFrame({'col':[np.nan, 0, 'foo']}, index=[2,1,0])
126127
df.equals(df2)

doc/source/whatsnew/v0.15.0.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ For full docs, see the :ref:`categorical introduction <categorical>` and the
6767
:ref:`API documentation <api.categorical>`.
6868

6969
.. ipython:: python
70-
70+
:okwarning:
71+
7172
df = DataFrame({"id":[1,2,3,4,5,6], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
7273

7374
df["grade"] = df["raw_grade"].astype("category")

doc/source/whatsnew/v0.7.3.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ When calling ``apply`` on a grouped Series, the return value will also be a
8383
Series, to be more consistent with the ``groupby`` behavior with DataFrame:
8484

8585
.. ipython:: python
86+
:okwarning:
8687

8788
df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
8889
'foo', 'bar', 'foo', 'foo'],
@@ -93,4 +94,3 @@ Series, to be more consistent with the ``groupby`` behavior with DataFrame:
9394
grouped = df.groupby('A')['C']
9495
grouped.describe()
9596
grouped.apply(lambda x: x.order()[-2:]) # top 2 values
96-

doc/source/whatsnew/v0.9.1.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ New features
2121
specified in a per-column manner to support multiple sort orders (:issue:`928`)
2222

2323
.. ipython:: python
24+
:okwarning:
2425

2526
df = DataFrame(np.random.randint(0, 2, (6, 3)), columns=['A', 'B', 'C'])
2627

@@ -66,7 +67,7 @@ New features
6667
.. ipython:: python
6768

6869
df[df>0]
69-
70+
7071
df.where(df>0)
7172

7273
df.where(df>0,-df)

0 commit comments

Comments
 (0)