Puzzle 11 Phil? Nah!?

Pandas Brain Teasers — by Miki Tebeka (19 / 34)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Free Range | TOC | Multiplying 👉

fillna.py

​ ​import​ ​numpy​ ​as​ ​np​
​ ​import​ ​pandas​ ​as​ ​pd​

​ s = pd.Series([1, 2, np.nan, 4, 5])
​ s.fillna(3)
​ ​print​(s.sum())

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will print: 12.0

images/hline.png

The pandas.Series.fillna documentation says the following:

Returns: Series or None
Object with missing values filled or None if inplace=True.

It’s always a good idea to not change (mutate) an object passed to a function. On the other hand, Pandas tries to be efficient and not copy data around a lot.

The design decision for fillna, both in pandas.Series and pandas.DataFrame, was not to change the original object and return a copy. But the user has an option to pass inplace=True, and then the original object is changed.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.