Puzzle 12 Multiplying

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Phil? Nah!? | TOC | A 10% Discount 👉

mul.py

​ ​import​ ​pandas​ ​as​ ​pd​

​ v = pd.Series([.1, 1., 1.1])
​ out = v * v
​ expected = pd.Series([.01, 1., 1.21])
​ ​if​ (out == expected).all():
​ ​print​(​'Math rocks!'​)
​ ​else​:
​ ​print​(​'Please reinstall universe & reboot.'​)

Guess the Output

IMPORTANT

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

images/hline.png

This code will print: Please reinstall universe & reboot.

images/hline.png

out == expected returns a Boolean pandas.Series. The all method returns True if all elements are True.

When you look at out and expected, they seem the same:

​ In [1]: out
​ Out[1]:
​ 0 0.01
​ 1 1.00
​ 2 1.21
​ dtype: float64
​ In [2]: expected
​ Out[2]:
​ 0 0.01
​ 1 1.00
​ 2 1.21
​ dtype: float64

But when we compare, we see something strange:

​ In [2]: out == expected
​ Out[2]:
​ 0 False
​ 1 True
​ 2…

--

--

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.