Sorry I think I am missing something very basic here:
>>> Series([3,4,0,3]).sort()
outputs None, while
>>> Series([3,4,0,3]).order()
2 0
0 3
3 3
1 4
dtype: int64
what am I missing with sort()?
Thanks
EDIT:
Thanks for the answers, I do realize now that this is sorting in place. But I don't understand why
>>> s = Series([3,4,0,3]).sort()
>>> s
does not return the sorted Series. If I understand the manual it should return the series sorted in place.
.sort() sorts in-place.
That means that after you call .sort(), your existing array has been sorted. It doesn't return anything.
To take an example from "core" Python:
In [175]: L = [2, 3, 1, 5]
In [176]: L.sort()
In [177]: print(L)
[1, 2, 3, 5]
It's the same for Pandas, as documented by Pandas.sort:
Sort values and index labels by value, in place. For compatibility with ndarray API. No return value
See also: What's the difference between Series.sort() and Series.order()?
In [1]: import pandas as pd
In [2]: s = pd.Series([3,4,0,3]).sort()
In [3]: s
Indeed In [3] will output nothing, as you can check:
In [4]: type(s)
Out[4]: NoneType
The reason:
pd.Series([3,4,0,3]) indeed return a pandas Series type object, BUT Series.sort() method return nothing because of inplace sorting. So the expression s = pd.Series([3,4,0,3]).sort(), s in LHS get nothing from RHS, thus In [3]: s output nothing.
After version 0.17.0, sorting by value methods pandas.Series.sort() and pandas.Series.order() are DEPRECATED, replaced by a unified pandas.Series.sort_values() API. See this answer for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With