0
0
Data Analysis Pythondata~20 mins

Shift and lag operations in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shift and Lag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
0    10
1    20
2    30
3    40
4    50
dtype: int64
B
0     NaN
1     NaN
2    10.0
3    20.0
4    30.0
dtype: float64
C
0    30.0
1    40.0
2    50.0
3     NaN
4     NaN
dtype: float64
D
0    NaN
1    10.0
2    20.0
3    30.0
4    40.0
dtype: float64
Attempts:
2 left
💡 Hint
Shift moves values down by the number specified, filling new spots with NaN.
data_output
intermediate
2: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)
A
    A  lag_A
0   5      0
1  10      5
2  15     10
3  20     15
B
    A  lag_A
0   5    NaN
1  10      5
2  15     10
3  20     15
C
    A  lag_A
0   5      0
1  10      0
2  15      5
3  20     10
D
    A  lag_A
0   5      5
1  10     10
2  15     15
3  20     20
Attempts:
2 left
💡 Hint
The fill_value replaces NaN created by the shift.
visualization
advanced
2: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()
ALine plot with original series starting at 3 and shifted series starting with NaN at index 0
BBar plot with original and shifted series overlapping exactly
CScatter plot with shifted series values all zero
DLine plot with shifted series values ahead of original by one index
Attempts:
2 left
💡 Hint
Shifted series moves values down, so first value is missing (NaN).
🔧 Debug
advanced
2: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')
AKeyError because '2' is not a valid index
BValueError because string '2' is not a valid shift value
CNo error, shift converts string '2' to integer 2 automatically
DTypeError because shift expects an integer or timedelta, not a string
Attempts:
2 left
💡 Hint
Check the type of argument shift accepts.
🧠 Conceptual
expert
3: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?
AShift and lag are identical operations with different names in pandas
BShift moves data up or down by a specified number of periods, while lag always moves data backward by one period
CShift moves data by a specified number of periods and can move forward or backward; lag specifically refers to moving data backward by one or more periods
DLag moves data forward in time, shift moves data backward
Attempts:
2 left
💡 Hint
Think about direction and flexibility of shift vs lag.