0
0
Pandasdata~20 mins

dtypes and data type checking in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this dtype check?
Given the DataFrame below, what will be the output of df.dtypes?
Pandas
import pandas as pd

data = {'A': [1, 2, 3], 'B': [1.1, 2.2, 3.3], 'C': ['x', 'y', 'z']}
df = pd.DataFrame(data)
print(df.dtypes)
A
A      int64
B      int64
C     object
dtype: object
B
A    float64
B    float64
C    object
dtype: object
C
A      int64
B    float64
C     object
dtype: object
D
A      int32
B    float32
C     object
dtype: object
Attempts:
2 left
💡 Hint
Check the default integer and float types pandas uses on your system.
data_output
intermediate
2:00remaining
How many columns are numeric in this DataFrame?
Using the DataFrame below, how many columns will df.select_dtypes(include=['number']).shape[1] return?
Pandas
import pandas as pd
import numpy as np

data = {'X': [1, 2, 3], 'Y': [4.5, 5.5, 6.5], 'Z': ['a', 'b', 'c'], 'W': [True, False, True]}
df = pd.DataFrame(data)
print(df.select_dtypes(include=['number']).shape[1])
A2
B4
C1
D3
Attempts:
2 left
💡 Hint
Boolean columns are considered numeric by default in pandas.
🔧 Debug
advanced
2:00remaining
Why does this dtype conversion raise an error?
Consider this code snippet. Why does df['col'] = df['col'].astype(int) raise an error?
Pandas
import pandas as pd

data = {'col': ['1', '2', 'three', '4']}
df = pd.DataFrame(data)
df['col'] = df['col'].astype(int)
ABecause the string 'three' cannot be converted to an integer, causing a ValueError.
BBecause pandas does not allow converting strings to integers directly, causing a TypeError.
CBecause the column contains mixed types, causing a KeyError.
DBecause the DataFrame is missing a required index, causing an IndexError.
Attempts:
2 left
💡 Hint
Check the values in the column carefully for non-numeric strings.
🧠 Conceptual
advanced
2:00remaining
Which dtype is best for storing categorical data efficiently?
You have a column with repeated string values representing categories. Which dtype should you use to save memory and speed up operations?
Aobject
Bcategory
Cstring
Dint64
Attempts:
2 left
💡 Hint
Think about pandas special dtype for repeated categories.
🚀 Application
expert
3:00remaining
Identify the dtype of each column after this operation
After running the code below, what are the dtypes of columns 'A', 'B', and 'C'?
Pandas
import pandas as pd
import numpy as np

data = {'A': [1, 2, 3], 'B': [1.0, 2.0, 3.0], 'C': ['a', 'b', 'c']}
df = pd.DataFrame(data)
df['A'] = df['A'].astype('float32')
df['B'] = df['B'].astype('int32')
df['C'] = df['C'].astype('category')
print(df.dtypes)
A
A    float64
B      int32
C    category
dtype: object
B
A    float64
B      int64
C     object
dtype: object
C
A      int32
B    float32
C     object
dtype: object
D
A    float32
B      int32
C    category
dtype: object
Attempts:
2 left
💡 Hint
Check the explicit astype conversions carefully.