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

前端 未结 2 837
轮回少年
轮回少年 2021-02-20 08:00
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

2条回答
  •  别跟我提以往
    2021-02-20 08:29

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

    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)

    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.

    As noted in the question, the default values of the inplace parameter for sort inplace = True and order inplace = False are different but there is no other difference in behaviour.

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

提交回复
热议问题