pandas Series.sort() 和 Series.order() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23696834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:03:41  来源:igfitidea点击:

What's the difference between Series.sort() and Series.order()?

pythonpandas

提问by usual me

s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) )
s

Output

输出

1    3
7    6
2    0
9    7
1    6

order()sorts by value and returns a new Series

order()按值排序并返回一个新系列

s.order()

Output

输出

2    0
1    3
7    6
1    6
9    7

It looks like sortalso sorts by value, but in place:

它看起来sort也按值排序,但就位:

s.sort()
s

Output

输出

2    0
1    3
7    6
1    6
9    7

Is this the only difference between the two methods?

这是两种方法的唯一区别吗?

回答by YaOzI

Your Question: Is this (Series.sortin-place v.s. Series.orderreturn-new-obj) the only difference between the two methods?

您的问题:这是(Series.sort就地与Series.orderreturn-new-obj)这两种方法之间的唯一区别吗?

BEFORE pandas 0.17.0 Final release (i.e. before 2015-10-09)

在Pandas 0.17.0 最终版本之前(即 2015-10-09 之前)

Short Answer: YES. They are functionally equivalent.

简短回答:是的。它们在功能上是等效的。

Longer Answer:

更长的答案:

pandas.Series.sort(): change the object itself (in-place sorting), but returns nothing.

pandas.Series.sort():更改对象本身(就地排序),但不返回任何内容。

Sort values and index labels by value. This is an inplace sortby default. Series.orderis the equivalent but returns a new Series.

按值对值和索引标签进行排序。这是默认的就地排序Series.order是等价的,但返回一个新的系列。

So

所以

>>> s = pd.Series([3,4,0,3]).sort()
>>> s

outputs nothing. See the answer herefor more details.

什么都不输出。有关更多详细信息,请参阅此处的答案

pandas.Series.order(): dose not change the object, instead it returns a new sorted object.

pandas.Series.order(): 不要改变对象,而是返回一个新的排序对象。

Sorts Series object, by value, maintaining index-value link. This will return a new Seriesby default. Series.sortis the equivalent but as an inplace method.

按值排序 Series 对象,维护索引值链接。这将默认返回一个新系列Series.sort是等价的,但作为一个就地方法。



AFTER pandas 0.17.0 Final release (i.e. after 2015-10-09)

在Pandas 0.17.0 最终版本之后(即 2015-10-09 之后)

The API of sorting is changed, things became cleaner and more pleasant.

排序的 API发生了变化,事情变得更干净,更愉快。

To sort by the values, both Series.sort()and Series.order()are DEPRECATED, replaced by the new Series.sort_values()api, which returns a sorted Series object.

要按排序,Series.sort()Series.order()都是DEPRECATED,替换为新的Series.sort_values()api,它返回一个排序的 Series 对象。

To summary the changes (excerpt from pandas 0.17.0 doc):

总结更改(摘自 pandas 0.17.0 doc):

To sort by the values (A * marks items that will show a FutureWarning):

        Previous              |         Replacement
------------------------------|-----------------------------------
* Series.order()              |  Series.sort_values()
* Series.sort()               |  Series.sort_values(inplace=True)
* DataFrame.sort(columns=...) |  DataFrame.sort_values(by=...) 

回答by undershock

Looking at the pandas source code (and skipping out the docstring)

查看 Pandas 源代码(并跳过文档字符串)

def sort(self, axis=0, ascending=True, kind='quicksort', na_position='last', inplace=True):
        return self.order(ascending=ascending,
                          kind=kind,
                          na_position=na_position,
                          inplace=inplace)

Compare this with the declaring line of order (I'm using 0.14.1)

将此与声明的顺序行进行比较(我使用的是 0.14.1)

def order(self, na_last=None, ascending=True, kind='quicksort', na_position='last', inplace=False)

You can see that since sort calls the order function the two are for all intents and purposes identical under the hood other than their default parameters.

您可以看到,由于 sort 调用 order 函数,因此除了默认参数之外,两者在所有意图和目的下都是相同的。

As noted in the question, the default values of the inplaceparameter for sort inplace = Trueand order inplace = Falseare different but there is no other difference in behaviour.

如问题中所述inplace, sortinplace = True和 order参数的默认值inplace = False不同,但行为没有其他差异。

The other only other difference is that order has an additional (but deprecated) parameter in the form of na_lastwhich you cannot use with sort (and shouldn't be using anyway).

另一个唯一的区别是 order 有一个额外的(但已弃用)参数,其形式na_last不能与 sort 一起使用(并且无论如何都不应该使用)。