0
0
Pandasdata~20 mins

shift() for lagging data in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shift Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of shift() with default parameters
What is the output of the following code snippet using pandas shift() with default lag?
Pandas
import pandas as pd

df = pd.DataFrame({'value': [10, 20, 30, 40]})
df['lagged'] = df['value'].shift()
print(df)
A
   value  lagged
0     10     NaN
1     20    10.0
2     30    20.0
3     40    30.0
B
   value  lagged
0     10    10.0
1     20    20.0
2     30    30.0
3     40    40.0
C
   value  lagged
0     10    20.0
1     20    30.0
2     30    40.0
3     40     NaN
D
   value  lagged
0     10     NaN
1     20     NaN
2     30     NaN
3     40     NaN
Attempts:
2 left
💡 Hint
Remember that shift() moves data down by 1 row by default, introducing NaN at the top.
data_output
intermediate
2:00remaining
Result of shift() with negative periods
What is the resulting DataFrame after applying shift() with periods=-1 on the 'value' column?
Pandas
import pandas as pd

df = pd.DataFrame({'value': [5, 15, 25, 35]})
df['lead'] = df['value'].shift(periods=-1)
print(df)
A
   value  lead
0      5  35.0
1     15  25.0
2     25  15.0
3     35   NaN
B
   value  lead
0      5   NaN
1     15   5.0
2     25  15.0
3     35  25.0
C
   value  lead
0      5  15.0
1     15  25.0
2     25  35.0
3     35   NaN
D
   value  lead
0      5   NaN
1     15   NaN
2     25   NaN
3     35   NaN
Attempts:
2 left
💡 Hint
Negative periods shift data up, so the last row becomes NaN.
🔧 Debug
advanced
2:00remaining
Identify the error in shift() usage
What error will this code raise and why? import pandas as pd df = pd.DataFrame({'score': [100, 200, 300]}) df['shifted'] = df['score'].shift(periods='2')
Pandas
import pandas as pd

df = pd.DataFrame({'score': [100, 200, 300]})
df['shifted'] = df['score'].shift(periods='2')
ATypeError: unsupported operand type(s) for +: 'int' and 'str'
BSyntaxError: invalid syntax
CNo error, code runs successfully
DValueError: periods must be an integer
Attempts:
2 left
💡 Hint
Check the type of the 'periods' argument in shift().
🚀 Application
advanced
2:30remaining
Using shift() to calculate daily returns
Given a DataFrame with daily stock prices, which code correctly calculates the daily return as percentage change using shift()?
Pandas
import pandas as pd

df = pd.DataFrame({'price': [100, 105, 110, 120]})
Adf['return'] = df['price'] - df['price'].shift(1) * 100
Bdf['return'] = (df['price'] - df['price'].shift(1)) / df['price'].shift(1) * 100
Cdf['return'] = (df['price'] / df['price'].shift(1)) * 100
Ddf['return'] = (df['price'].shift(1) - df['price']) / df['price'] * 100
Attempts:
2 left
💡 Hint
Daily return is (today's price - yesterday's price) divided by yesterday's price.
🧠 Conceptual
expert
3:00remaining
Effect of shift() on time series with missing dates
If a time series DataFrame has missing dates and you apply shift() on the value column, what is the effect on the lagged data?
AThe lagged data aligns with the previous row regardless of missing dates, so gaps remain unfilled.
Bshift() automatically fills missing dates with NaN before shifting, so lagged data aligns with actual previous dates.
Cshift() fills missing dates with the last available value before shifting, so lagged data has no gaps.
Dshift() removes rows with missing dates before shifting, so lagged data only contains continuous dates.
Attempts:
2 left
💡 Hint
shift() works row-wise and does not modify the index or fill missing dates.