Challenge - 5 Problems
Dtype Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this dtype check?
Given the following pandas DataFrame, what will be the output of
df.dtypes?Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [1.5, 2.5, 3.5], 'C': ['x', 'y', 'z'], 'D': [True, False, True] }) print(df.dtypes)
Attempts:
2 left
💡 Hint
Check the default integer type pandas uses on your system and how strings are stored.
✗ Incorrect
Column 'A' contains integers, so pandas uses int64 by default. Column 'B' has floats, so float64. Column 'C' has strings, stored as object dtype. Column 'D' has booleans, so bool dtype.
❓ data_output
intermediate2:00remaining
Count columns with numeric dtypes
Using pandas, how many columns in this DataFrame have numeric data types?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({ 'num1': [1, 2, 3], 'num2': [1.1, 2.2, 3.3], 'text': ['a', 'b', 'c'], 'flag': [True, False, True], 'date': pd.to_datetime(['2023-01-01', '2023-01-02', '2023-01-03']) }) numeric_cols = df.select_dtypes(include=[np.number]).columns print(len(numeric_cols))
Attempts:
2 left
💡 Hint
Numeric types include int and float, but not boolean or datetime.
✗ Incorrect
Only 'num1' (int) and 'num2' (float) are numeric. 'flag' is boolean, 'text' is object, 'date' is datetime64, so not counted.
🔧 Debug
advanced2:00remaining
Identify the error in dtype conversion
What error will this code raise when trying to convert column 'A' to integer dtype?
Pandas
import pandas as pd df = pd.DataFrame({'A': ['1', '2', 'three']}) df['A'] = df['A'].astype(int)
Attempts:
2 left
💡 Hint
Check if all strings can be converted to integers.
✗ Incorrect
The string 'three' cannot be converted to an integer, causing a ValueError.
🚀 Application
advanced2:00remaining
Select columns with object dtype
Given a DataFrame, which code snippet correctly selects all columns with object dtype?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2], 'B': ['x', 'y'], 'C': [3.5, 4.5], 'D': ['foo', 'bar'] })
Attempts:
2 left
💡 Hint
Object dtype usually stores strings or mixed types.
✗ Incorrect
Only option B selects columns with object dtype, which are 'B' and 'D'.
🧠 Conceptual
expert2:00remaining
Understanding pandas dtype memory optimization
Which pandas dtype conversion reduces memory usage the most for a column of integers ranging from 0 to 127?
Attempts:
2 left
💡 Hint
Consider the smallest integer dtype that can hold values 0 to 127.
✗ Incorrect
int8 can hold values from -128 to 127, sufficient for 0 to 127. It uses 1 byte per value, less than float32 (4 bytes), int64 (8 bytes), or object (most).