All sample questions / pandas

What does print((a + b).tolist()) print? pandas Missing Data, NaN and NA

Block 3, Missing Data, NaN and NA, question 8 of the pandas sample

import pandas as pd
a = pd.array([1, pd.NA, 3], dtype='Int64')
b = pd.array([1, 2, pd.NA], dtype='Int64')
print((a + b).tolist())

What does this print?

  1. A. [<NA>, <NA>, <NA>]
  2. B. [2, <NA>, <NA>]
  3. C. [2, 2, <NA>]
  4. D. [2, 2, 3]
Show the answer and why each option is right or wrong

Answer, B. [2, <NA>, <NA>]

A. [<NA>, <NA>, <NA>]
Assumes a single missing value anywhere in either array poisons every position of the result rather than only the ones it actually touches.
B. [2, <NA>, <NA>]
Correct. Elementwise addition of two nullable Int64 arrays only produces a real value where both sides are present, so the first position gives 2, but the second and third each have one missing side and come back as <NA>.
C. [2, 2, <NA>]
Assumes a real value paired with a missing one on the other array still produces a real number instead of propagating the missing marker.
D. [2, 2, 3]
Assumes the missing slot on either side of the addition is simply ignored, as though it contributed nothing rather than making the result missing.

Elementwise arithmetic between two nullable arrays follows pd.NA propagation position by position, a missing value on either side of a given position makes that one result missing, and it has no effect on the other positions. This is different from a whole series reduction like sum(), where skipna controls whether a single missing value anywhere poisons the entire aggregate.

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.