All sample questions / pandas
pandas Missing Data, NaN and NA, what does print(s.tolist()) print after s = pd.Series([1, None, 3])?
Block 3, Missing Data, NaN and NA, question 7 of the pandas sample
import pandas as pd
s = pd.Series([1, None, 3])
s2 = s.fillna(0)
print(s.tolist())
What does this print?
Show the answer and why each option is right or wrong
Answer, A. [1.0, nan, 3.0]
- A. [1.0, nan, 3.0]
- Correct. fillna() returns a new series by default, leaving the original one untouched, so s still holds its original missing value, printed as nan since the series is float64.
- B. [1, None, 3]
- Assumes the original series keeps its pre missing value dtype and the None marker rather than the float nan it actually holds.
- C. [1.0, 0.0, 3.0]
- Assumes fillna() changes the original series in place instead of returning a separate filled copy.
- D. [1.0, 3.0]
- Assumes building the filled copy also removes the missing value from the original series.
Unless inplace=True is passed, fillna() and most other pandas methods return a new object and leave the one they were called on unchanged. The original series here was already float64 because of the earlier missing value, so its untouched entry still prints as nan, not as None.
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.