0
0
Pandasdata~20 mins

Common dtype errors and fixes in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
dtype_master
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 conversion code?
Consider the following pandas code that attempts to convert a column to numeric. What will be the output of df['A'] after running this code?
Pandas
import pandas as pd

df = pd.DataFrame({'A': ['1', '2', 'three', '4']})
df['A'] = pd.to_numeric(df['A'], errors='coerce')
print(df['A'])
ARaises ValueError due to 'three' string
B
0       1
1       2
2    three
3       4
Name: A, dtype: object
C
0    1.0
1    2.0
2    NaN
3    4.0
Name: A, dtype: float64
D
0    1
1    2
2    0
3    4
Name: A, dtype: int64
Attempts:
2 left
💡 Hint
Look at how errors='coerce' handles non-numeric values.
data_output
intermediate
1:30remaining
How many unique values are in the column after conversion?
Given this code, how many unique values does the column 'B' have after conversion?
Pandas
import pandas as pd

df = pd.DataFrame({'B': ['10', '20', '30', '20', 'ten']})
df['B'] = pd.to_numeric(df['B'], errors='coerce')
unique_count = df['B'].nunique(dropna=True)
print(unique_count)
A3
B4
C5
DRaises TypeError
Attempts:
2 left
💡 Hint
Remember that nunique(dropna=True) excludes NaN values.
🔧 Debug
advanced
2:00remaining
Why does this code raise a ValueError?
This code tries to convert a column to datetime but raises a ValueError. What is the cause?
Pandas
import pandas as pd

df = pd.DataFrame({'dates': ['2023-01-01', 'not_a_date', '2023-03-01']})
df['dates'] = pd.to_datetime(df['dates'], format='%Y-%m-%d')
ABecause the format string is incorrect for the given dates
BBecause 'not_a_date' does not match the format and no error handling is set
CBecause pd.to_datetime cannot convert strings to datetime
DBecause the DataFrame column is not of string type
Attempts:
2 left
💡 Hint
Check how pd.to_datetime behaves when a string does not match the format and no errors parameter is given.
🚀 Application
advanced
2:30remaining
Which option correctly converts a mixed-type column to numeric with invalid values as zero?
You have a pandas column with numbers as strings and some invalid strings. You want to convert it to numeric, replacing invalid values with zero. Which code does this correctly?
Pandas
import pandas as pd

df = pd.DataFrame({'vals': ['5', '10', 'abc', '20']})
Adf['vals'] = pd.to_numeric(df['vals'], errors='raise')
Bdf['vals'] = pd.to_numeric(df['vals'], errors='ignore').fillna(0)
Cdf['vals'] = df['vals'].astype(int)
Ddf['vals'] = pd.to_numeric(df['vals'], errors='coerce').fillna(0).astype(int)
Attempts:
2 left
💡 Hint
Think about how to handle invalid strings and convert the column to integers.
🧠 Conceptual
expert
3:00remaining
What is the main reason pandas converts integer columns with NaN to float dtype?
In pandas, when you convert a column with integers but some values are missing (NaN), the column dtype changes from int to float. Why does this happen?
ABecause NaN is a float value and integer dtype cannot represent NaN
BBecause pandas automatically converts all columns with missing values to float for performance
CBecause integers are stored as floats internally in pandas
DBecause NaN is treated as zero in integer columns
Attempts:
2 left
💡 Hint
Think about how missing values are represented in pandas and numpy.