0
0
Pandasdata~20 mins

Interpolation for missing values in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of linear interpolation on a DataFrame column
What is the output of the following code snippet that uses linear interpolation to fill missing values in a pandas DataFrame column?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, np.nan, 4, 5]})
df['A'] = df['A'].interpolate(method='linear')
print(df['A'].tolist())
A[1.0, nan, nan, 4.0, 5.0]
B[1.0, 2.0, 3.0, 4.0, 5.0]
C[1.0, 2.5, 3.5, 4.0, 5.0]
D[1.0, 1.0, 1.0, 4.0, 5.0]
Attempts:
2 left
💡 Hint
Linear interpolation fills missing values by connecting points with a straight line.
data_output
intermediate
2:00remaining
Result of time interpolation on a time-indexed DataFrame
Given a DataFrame indexed by dates with missing values, what is the result of using time-based interpolation?
Pandas
import pandas as pd
import numpy as np
idx = pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-04', '2024-01-05'])
df = pd.DataFrame({'Value': [10, np.nan, 40, 50]}, index=idx)
df_interpolated = df.interpolate(method='time')
print(df_interpolated['Value'].tolist())
A[10.0, 25.0, 40.0, 50.0]
B[10.0, 30.0, 40.0, 50.0]
C[10.0, nan, 40.0, 50.0]
D[10.0, 20.0, 40.0, 50.0]
Attempts:
2 left
💡 Hint
Time interpolation considers the time difference between index points.
🔧 Debug
advanced
2:00remaining
Identify the error in interpolation code
What error will this code raise when trying to interpolate missing values in a DataFrame?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3]})
df['A'] = df['A'].interpolate(method='polynomial', order=2)
print(df)
ANo error, outputs interpolated DataFrame
BTypeError: interpolate() got an unexpected keyword argument 'order'
CValueError: method 'polynomial' requires specifying 'order' parameter
DAttributeError: 'Series' object has no attribute 'interpolate'
Attempts:
2 left
💡 Hint
Check if the 'order' parameter is correctly passed for polynomial interpolation.
🚀 Application
advanced
2:00remaining
Choosing interpolation method for categorical data
You have a DataFrame column with missing categorical values like ['red', NaN, 'blue', NaN, 'green']. Which interpolation method is appropriate to fill missing values?
AUse method='polynomial' with order=2 for smooth category interpolation
BUse method='linear' to interpolate categories as numeric values
CUse method='nearest' to fill missing values with the closest non-missing category
DUse method='time' assuming the index is datetime
Attempts:
2 left
💡 Hint
Categorical data cannot be interpolated numerically.
🧠 Conceptual
expert
2:00remaining
Effect of limit parameter in interpolation
What is the effect of setting the 'limit' parameter to 1 in pandas interpolate method when filling missing values?
AIt limits the maximum number of consecutive NaNs to fill to 1, leaving longer gaps unfilled
BIt limits the total number of NaNs filled in the entire DataFrame to 1
CIt limits the interpolation to only the first missing value in the column
DIt limits the interpolation to only numeric columns
Attempts:
2 left
💡 Hint
Think about how 'limit' controls consecutive missing values filled.