Skip to content

sort seems to sort inplace by default unlike documentation #10522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
s-celles opened this issue Jul 7, 2015 · 3 comments
Closed

sort seems to sort inplace by default unlike documentation #10522

s-celles opened this issue Jul 7, 2015 · 3 comments

Comments

@s-celles
Copy link
Contributor

s-celles commented Jul 7, 2015

Hello,

I noticed

df_ohlc['high'].sort(ascending=False)

raises

ValueError: This Series is a view of some other array, to sort in-place you must create a copy

which seems to mean that, by default, sort uses by default inplace=True

but according to

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort.html

inplace : boolean, default False

Here is a minimal example:

import pandas as pd
import numpy as np
N = 1000
rng = pd.date_range('1/1/2011', periods=N, freq='H')
s = pd.Series(np.random.random(N), index=rng)
df_ohlc = s.resample('1D', how='ohlc')
df_ohlc['high'].sort(ascending=False)

raises ValueError: This Series is a view of some other array, to sort in-place you must create a copy

df_ohlc['high'].sort(ascending=False, inplace=True)

raises same exception

but

df_ohlc['high'].sort(ascending=False, inplace=False)

doesn't raises exception

Kind regards

@jreback
Copy link
Contributor

jreback commented Jul 7, 2015

you are sorting a Series, not a frame
.sort defaults to True for a Series

@jreback jreback closed this as completed Jul 7, 2015
@s-celles
Copy link
Contributor Author

s-celles commented Jul 7, 2015

Thanks
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.sort.html

it's odd to have by default inplace=True for Series and inplace=False for DataFrame

Now I will remember.

Sorry

@jorisvandenbossche
Copy link
Member

It's indeed odd, and there are already a lot of discussions to have this solved (see eg #8239 and #9816), but we can't just change it for backward compatible reasons.

@jorisvandenbossche jorisvandenbossche added this to the No action milestone Jul 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants