B. Cannot convert from category back to int16 directly.
C. astype() cannot be used multiple times on the same column.
D. DataFrame creation syntax is incorrect.
Solution
Step 1: Check data type conversions
Converting from int8 to category is valid, but category stores codes, not original ints.
Step 2: Conversion from category to int16
Directly converting category to int16 causes error because category is not numeric type.
Final Answer:
Cannot convert from category back to int16 directly. -> Option B
Quick Check:
Category to int conversion invalid [OK]
Hint: Category to int needs .cat.codes first [OK]
Common Mistakes:
Assuming int8 is invalid
Thinking astype can chain without issues
Believing DataFrame creation is wrong
5. You have a DataFrame with a column 'status' containing only three unique string values repeated many times. Which is the best way to optimize memory usage for this column while keeping it easy to analyze?
hard
A. Leave 'status' as object type because conversion wastes time.
B. Convert 'status' to integer type directly with astype('int32').
C. Convert 'status' to a categorical type using astype('category').
D. Convert 'status' to float type with astype('float64').
Solution
Step 1: Analyze the column data
'status' has few unique strings repeated many times, ideal for categorical type.
Step 2: Choose the best optimization
astype('category') reduces memory by storing unique values once and codes for repeats.
Final Answer:
Convert 'status' to a categorical type using astype('category'). -> Option C
Quick Check:
Few unique strings = use category [OK]
Hint: Few unique strings? Use category type [OK]
Common Mistakes:
Trying to convert strings directly to int or float
Ignoring memory benefits of categorical
Thinking object type is always best
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.