1. Home
  2. Data Science
  3. Pandas
  4. What does pd.to_numeric(s, errors='coerce') do with…

Added 30 Nov -0001

What does pd.to_numeric(s, errors='coerce') do with entries that cannot be parsed?

  1. It converts the bad entries to zero
  2. It replaces them with NaN and returns a numeric dtype column Correct answer
  3. It leaves the column as object and keeps the original strings
  4. It raises a ValueError listing the offending values

Correct answer

B. It replaces them with NaN and returns a numeric dtype column

14 views

MCQSEXAM

Explanation

Coercion is the middle ground between errors='raise', which throws, and errors='ignore', which returns the input unchanged and can leave a mixed object column. Coerce turns unparseable entries into NaN, producing a float or nullable numeric dtype suitable for arithmetic. Count the new NaNs afterwards, because coercion can hide genuinely malformed data.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs