All sample questions / pandas

pandas Series, Index and dtypes, what does print(s.tolist()) print after s = pd.Series([1, True])?

Block 1, Series, Index and dtypes, question 1 of the pandas sample

import pandas as pd
s = pd.Series([1, True])
print(s.tolist())

What does this print?

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

Answer, B. [1, True]

A. [1, 1]
Assumes pandas coerces the bool into a plain number the way it would for a numpy array, collapsing True down to 1.
B. [1, True]
Correct. Pandas keeps a bool mixed with an int as separate Python objects rather than silently coercing the bool to a number, so the original int 1 and the original True come back unchanged.
C. [True, True]
Assumes the int gets coerced up to a bool instead, turning the 1 into True.
D. [1.0, 1.0]
Assumes the mix gets promoted to a floating point dtype the way an int and a missing value would.

A plain numpy array silently treats True as 1, but a pandas Series holding a genuine mix of int and bool falls back to the object dtype and keeps every value as the exact Python object it started as. Nothing here gets coerced to a common numeric type.

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.

Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.