All sample questions / pandas
pandas Missing Data, NaN and NA, what does print(s.sum()) print after s = pd.Series([1, 2, None])?
Block 3, Missing Data, NaN and NA, question 6 of the pandas sample
import pandas as pd
s = pd.Series([1, 2, None])
print(s.sum())
What does this print?
Show the answer and why each option is right or wrong
Answer, C. 3.0
- A. 2.0
- Drops one of the two real values along with the missing one.
- B. 3
- Assumes the result stays a plain integer even though the series holding it became float once a missing value was introduced.
- C. 3.0
- Correct. sum() skips missing values by default, so it adds 1 and 2, and the result prints as 3.0 because the series became float the moment None entered it.
- D. nan
- Assumes a single missing value forces the whole sum to come back missing, the way it would with skipna turned off.
By default, sum(), mean() and most other reductions in pandas ignore missing values rather than propagating them. The dtype of the series is decided when it is built, not when it is summed, so a series that became float because of a None still reports its sum as a float.
The answer above is what pandas 3.0.5 printed when the snippet was run, not what someone expected it to do.
This question comes from a bank of 255 pandas interview questions, all built the same way, sold as one PDF with the full answer key.
Get the 255 pandas questions, 9 euros
Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros
Or play the free pandas sample, twenty questions in the browser, also as a free PDF.
Previous question Next question
Did this save you a lookup? Buy me a coffee on Ko-fi.