0
0
Pandasdata~20 mins

pct_change() for percentage change in Pandas - Practice Problems & Coding Challenges

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

s = pd.Series([100, 120, 144, 172.8])
result = s.pct_change()
print(result)
A
0    NaN
1    1.20
2    1.20
3    1.20
dtype: float64
B
0    NaN
1    0.20
2    0.20
3    0.20
dtype: float64
C
0    0.00
1    0.20
2    0.20
3    0.20
dtype: float64
D
0    NaN
1    20.0
2    20.0
3    20.0
dtype: float64
Attempts:
2 left
💡 Hint
Remember pct_change() calculates the relative change between current and prior element.
data_output
intermediate
2:00remaining
Percentage change with periods parameter
Given the DataFrame below, what is the output of df['value'].pct_change(periods=2)?
Pandas
import pandas as pd

df = pd.DataFrame({'value': [10, 15, 20, 30, 45]})
result = df['value'].pct_change(periods=2)
print(result)
A
0         NaN
1         NaN
2    2.000000
3    2.000000
4    2.250000
Name: value, dtype: float64
B
0         NaN
1         NaN
2    0.500000
3    0.500000
4    0.500000
Name: value, dtype: float64
C
0         NaN
1    0.500000
2    0.333333
3    0.500000
4    0.500000
Name: value, dtype: float64
D
0         NaN
1         NaN
2    1.000000
3    1.000000
4    1.250000
Name: value, dtype: float64
Attempts:
2 left
💡 Hint
The periods=2 means compare with the value two rows before.
🔧 Debug
advanced
2:00remaining
Identify the error in pct_change usage
What error does the following code raise?
Pandas
import pandas as pd

s = pd.Series([1, 2, 3, 4])
result = s.pct_change(periods='two')
print(result)
AValueError: periods must be an integer
BAttributeError: 'str' object has no attribute 'pct_change'
CTypeError: unsupported operand type(s) for -: 'str' and 'int'
DNo error, outputs percentage change
Attempts:
2 left
💡 Hint
Check the type of the periods parameter in pct_change.
🚀 Application
advanced
2:00remaining
Calculate daily returns from stock prices
You have a DataFrame with daily closing prices of a stock. Which code correctly calculates the daily returns as percentage changes?
Pandas
import pandas as pd

df = pd.DataFrame({'close': [100, 105, 110, 121, 115]})
Adf['returns'] = df['close'].pct_change()
Bdf['returns'] = df['close'].diff()
Cdf['returns'] = df['close'].pct_change().fillna(0)
Ddf['returns'] = (df['close'] / df['close'].shift(1)) * 100
Attempts:
2 left
💡 Hint
Daily returns should be percentage changes, and missing values can be filled with zero.
🧠 Conceptual
expert
2:00remaining
Understanding pct_change with fill_method parameter
What is the effect of using fill_method='ffill' in pct_change() on a Series with missing values?
AIt fills missing values forward before calculating percentage change, reducing NaNs in output.
BIt fills missing values backward after calculating percentage change, increasing NaNs in output.
CIt drops missing values before calculating percentage change, causing shorter output.
DIt replaces missing values with zero before calculating percentage change, causing division errors.
Attempts:
2 left
💡 Hint
Think about how forward fill affects data before pct_change calculation.