DataFrame.sort_index
is following the Command-Query separation principle which says that a function should either be a command or a query but not both.
- Commands do something such as modify the caller (e.g. the DataFrame) and should return
None
. (In languages that allow it, commands should return nothing. But since all functions return something in Python, the convention is to return None
.)
- Queries compute and return a result but do not modify the caller.
inplace=True
makes sort_index
a command. The method modifies the calling DataFrame and in keeping with the Command-Query principle, returns None
.
inplace=False
(the default) makes sort_index
a query.
Thus, if you want sort_index
to return a DataFrame, remove the inplace=True
.
Truly "inplace" operations like list.sort
modify the original data without using auxilliary data structures. In this sense, no Pandas operation is truly inplace. When inplace=True
is used, the result is first computed in an auxilliary data structure (e.g. another DataFrame), and then the underlying data is copied into the shell of the original DataFrame. So
df.sort_level(inplace=True)
is equivalent to
df = df.sort_level()
inplace=True
exists mainly because of the inertia of history.
The lead developer of Pandas, Jeff Reback says:
My personal opinion: I never use in-place operations. The syntax is harder to read and its does not offer any advantages.