Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Array Data Types
Identify the error in this code snippet:
import numpy as np
arr = np.array([1, 0, 1])
bool_arr = arr.astype(Boolean)
AMissing parentheses in astype()
Barr should be a list, not array
C'Boolean' is not a valid dtype in numpy
Dastype() cannot convert integers
Step-by-Step Solution
Solution:
  1. Step 1: Check dtype argument in astype()

    Valid dtype for boolean is 'bool' or np.bool_, not 'Boolean'.
  2. Step 2: Identify error cause

    Using 'Boolean' causes a TypeError because numpy does not recognize it.
  3. Final Answer:

    'Boolean' is not a valid dtype in numpy -> Option C
  4. Quick Check:

    Correct dtype = bool, not 'Boolean' [OK]
Quick Trick: Use 'bool' dtype, not 'Boolean' in astype() [OK]
Common Mistakes:
  • Using 'Boolean' instead of 'bool'
  • Thinking astype() needs no arguments
  • Confusing list and array types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes