Home > Blockchain >  why is inplace=True making a bug in sort_values of pandas
why is inplace=True making a bug in sort_values of pandas

Time:03-20

I'm new to pandas, and I know using of inplace=True is deprecated, but I wanna know why my first code doesn't prompt any error:

google = pd.read_csv("google_stock_price.csv").squeeze()
google = google.sort_values()
google.sort_values(ascending = False, inplace = True)

but if I delete the second line, I will get an error with this code:

google = pd.read_csv("google_stock_price.csv").squeeze()
google.sort_values(ascending = False, inplace = True)

the error is: enter image description here

CodePudding user response:

Your sort_values function must have an parameter passed to the by argument.

Please check this documentation and study the by argument. It must take either row or column names, which again changes by axis. There is no default value for by which means you have to pass a parameter if you do not want an error thrown. I am not aware of inplace being deprecated but some do discourage its use.

CodePudding user response:

The error message is telling you the problem. The in-place sort does not work on views of arrays. If you check the documentation of the squeeze() function, it probably returns a view of the imported csv. As the message states, copy() the squeezed array first.

  • Related