Challenge - 5 Problems
Diff Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of diff() with default parameters
What is the output of the following code snippet using pandas diff() with default settings?
Pandas
import pandas as pd s = pd.Series([10, 15, 20, 25]) result = s.diff() print(result)
Attempts:
2 left
💡 Hint
Remember diff() calculates the difference between the current and previous element by default.
✗ Incorrect
The diff() function by default calculates the difference between the current value and the previous value (periods=1). The first element has no previous value, so it is NaN.
❓ data_output
intermediate2:00remaining
Effect of periods parameter in diff()
Given the DataFrame below, what is the output of df['A'].diff(periods=2)?
Pandas
import pandas as pd df = pd.DataFrame({'A': [3, 8, 15, 24, 35]}) result = df['A'].diff(periods=2) print(result)
Attempts:
2 left
💡 Hint
The periods parameter defines how many rows back to subtract from the current row.
✗ Incorrect
diff(periods=2) subtracts the value two rows before from the current row. The first two rows have no value two rows before, so they are NaN.
🔧 Debug
advanced2:00remaining
Identify the error in diff() usage
What error will the following code produce and why?
Pandas
import pandas as pd s = pd.Series([1, 2, 3]) result = s.diff(periods='two') print(result)
Attempts:
2 left
💡 Hint
Check the type of the periods argument.
✗ Incorrect
The periods parameter must be an integer. Passing a string causes a ValueError.
🚀 Application
advanced2:00remaining
Using diff() to find daily sales changes
You have a DataFrame with daily sales data. Which code snippet correctly adds a new column 'daily_change' showing the difference in sales from the previous day?
Pandas
import pandas as pd df = pd.DataFrame({'date': ['2024-01-01', '2024-01-02', '2024-01-03'], 'sales': [100, 150, 130]})
Attempts:
2 left
💡 Hint
Default diff() compares current row with previous row.
✗ Incorrect
diff() with default periods=1 calculates difference from previous row, which is what daily change means.
🧠 Conceptual
expert2:00remaining
Understanding diff() behavior with non-numeric data
What happens when you apply diff() on a pandas Series containing strings?
Pandas
import pandas as pd s = pd.Series(['a', 'b', 'c']) result = s.diff()
Attempts:
2 left
💡 Hint
diff() uses subtraction internally.
✗ Incorrect
diff() tries to subtract consecutive values. Strings cannot be subtracted, so it raises a TypeError.