PythonDataScienceHandbook icon indicating copy to clipboard operation
PythonDataScienceHandbook copied to clipboard

Series output

Open ameliameyer opened this issue 3 years ago • 2 comments

Under Ch 3: Introducing Pandas Objects, the output shown is different than the output I get when I run the same line of code:

As written in the book:

pd.Series({2:'a', 1:'b', 3:'c'}) 1 b 2 a 3 c dtype: object

Whereas when I run the code:

pd.Series({2:'a', 1:'b', 3:'c'}) 2 a 1 b 3 c dtype: object

Is there something weird going on with my system or is there an error in the text?

ameliameyer avatar Jan 18 '22 08:01 ameliameyer

Hi, Probably its because of different version of python or pandas library which now not allow default sorting. Now, if you want above output then you have to use different command.

nivid26 avatar Feb 22 '22 19:02 nivid26

Following is from panda release note https://pandas.pydata.org/docs/whatsnew/v0.23.0.html#whatsnew-0230-api-breaking-dict-insertion-order

Python version 3.6 and later, dicts are ordered by insertion order, see PEP 468

If you wish to retain the old behavior while using Python >= 3.6, you can use .sort_index():

pd.Series({2:'a', 1:'b', 3:'c'}).sort_index()

pcoates33 avatar Sep 05 '22 16:09 pcoates33