1. Home
  2. Data Science
  3. Pandas
  4. A column holds integers and NaN. Why does astype('int64')…

Added 30 Nov -0001

A column holds integers and NaN. Why does astype('int64') fail, and what is the usual fix?

  1. The cast truncates NaN to zero, raising a warning rather than an error
  2. The fix is to cast to float16 first, because float16 supports missing values natively
  3. int64 has no NaN representation, so the nullable Int64 dtype must be used instead Correct answer
  4. int64 holds NaN perfectly well, but the cast is refused because of an unrelated mismatch

Correct answer

C. int64 has no NaN representation, so the nullable Int64 dtype must be used instead

15 views

MCQSEXAM

Explanation

Nullable integer dtypes such as Int64 keep a separate mask for missing values, which the fixed-width NumPy int64 cannot do, so astype('int64') raises when NaN is present. The alternatives are fillna with a sentinel, dropna, or converting to Int64 and letting downstream code handle pd.NA. Capitalisation matters: Int64 with a capital I is the extension type, int64 the NumPy one.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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