Challenge - 5 Problems
Shift and Lag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of shift operation on a pandas Series
What is the output of the following code snippet?
Data Analysis Python
import pandas as pd s = pd.Series([10, 20, 30, 40, 50]) result = s.shift(2) print(result)
Attempts:
2 left
💡 Hint
Shift moves values down by the number specified, filling new spots with NaN.
✗ Incorrect
The shift(2) moves all values down by 2 places. The first two positions become NaN, and the rest are the original values shifted down.
❓ data_output
intermediate2:00remaining
Result of lag operation with fill value
Given the DataFrame below, what is the output of the lag operation with fill value 0?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'A': [5, 10, 15, 20]}) df['lag_A'] = df['A'].shift(1, fill_value=0) print(df)
Attempts:
2 left
💡 Hint
The fill_value replaces NaN created by the shift.
✗ Incorrect
The shift(1) moves values down by 1. The first row gets the fill_value 0 instead of NaN.
❓ visualization
advanced2:30remaining
Visualizing shift effect on time series data
Which plot correctly shows the original and shifted time series data?
Data Analysis Python
import pandas as pd import matplotlib.pyplot as plt s = pd.Series([3, 6, 9, 12, 15]) s_shifted = s.shift(1) plt.plot(s, label='Original') plt.plot(s_shifted, label='Shifted') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Shifted series moves values down, so first value is missing (NaN).
✗ Incorrect
The line plot shows original values starting at index 0, shifted values start with NaN at index 0 and values from original shifted down by one index.
🔧 Debug
advanced2:00remaining
Identify error in shift usage
What error will this code raise and why?
import pandas as pd
s = pd.Series([1, 2, 3])
result = s.shift('2')
Data Analysis Python
import pandas as pd s = pd.Series([1, 2, 3]) result = s.shift('2')
Attempts:
2 left
💡 Hint
Check the type of argument shift accepts.
✗ Incorrect
shift expects an integer or timedelta. Passing a string causes a TypeError.
🧠 Conceptual
expert3:00remaining
Understanding difference between shift and lag in time series
Which statement best describes the difference between shift and lag operations in time series data?
Attempts:
2 left
💡 Hint
Think about direction and flexibility of shift vs lag.
✗ Incorrect
Shift is a general operation that can move data forward or backward by any number of periods. Lag is a specific case of shift moving data backward (to past periods).