pct_change() for percentage change in Pandas - Time & Space Complexity
We want to understand how the time needed to calculate percentage changes grows as the data size grows.
How does the work increase when we have more rows in our data?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.Series([10, 20, 30, 40, 50])
pct_changes = data.pct_change()
print(pct_changes)
This code calculates the percentage change between each value and the previous one in a list of numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function looks at each element and compares it to the one before it.
- How many times: It does this once for every element except the first.
As the number of data points grows, the work grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 comparisons |
| 100 | About 99 comparisons |
| 1000 | About 999 comparisons |
Pattern observation: The work grows in a straight line with the number of data points.
Time Complexity: O(n)
This means the time to calculate percentage changes grows directly with the number of values.
[X] Wrong: "pct_change() compares every value to all previous values, so it takes much longer as data grows."
[OK] Correct: pct_change() only compares each value to the one right before it, so it only does one comparison per value, not many.
Understanding how simple operations like pct_change() scale helps you explain your code's efficiency clearly and confidently.
"What if pct_change() was asked to compare each value to the value two steps before instead of one? How would the time complexity change?"
Practice
pct_change() function in pandas do?Solution
Step 1: Understand the purpose of pct_change()
Thepct_change()function calculates how much values change in percentage from one row to the next.Step 2: Compare with other options
Options A, B, and C describe different pandas functions unrelated to percentage change.Final Answer:
Calculates the percentage change between consecutive rows. -> Option AQuick Check:
pct_change() = percentage change [OK]
- Confusing pct_change() with sum()
- Thinking it sorts data
- Assuming it removes missing values
pct_change() on a pandas DataFrame named df?Solution
Step 1: Recall pandas method syntax
In pandas,pct_change()is a method called on a DataFrame or Series object, so the correct syntax isdf.pct_change().Step 2: Check other options
Options A, B, and D are not valid pandas syntax or method names.Final Answer:
df.pct_change() -> Option AQuick Check:
Method called on DataFrame = df.pct_change() [OK]
- Using pct_change() as a standalone function
- Mixing method names
- Using incorrect method names
import pandas as pd
df = pd.DataFrame({'Sales': [100, 120, 150, 180]})
print(df.pct_change())What is the output?
Solution
Step 1: Calculate percentage changes row by row
From 100 to 120: (120-100)/100 = 0.2
From 120 to 150: (150-120)/120 = 0.25
From 150 to 180: (180-150)/150 = 0.2Step 2: Understand first row result
The first row has no previous row, so result is NaN.Final Answer:
NaN, 0.2, 0.25, 0.2 -> Option DQuick Check:
pct_change() = NaN, 0.2, 0.25, 0.2 [OK]
- Confusing absolute change with percent change
- Forgetting first row is NaN
- Mixing up order of results
import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30]})
result = df.pct_change(axis=1)
print(result)Solution
Step 1: Understand axis parameter in pct_change()
By default, pct_change() calculates percentage change down rows (axis=0). axis=1 calculates change across columns.Step 2: Check DataFrame shape and axis=1 effect
The DataFrame has only one column. pct_change(axis=1) produces NaN values everywhere since there are no multiple columns to compare, but the code runs without errors or exceptions.Final Answer:
The code is correct and will run without errors. -> Option BQuick Check:
axis=1 on single column = NaN output, no error [OK]
- Thinking axis=1 causes error on single-column DataFrame
- Believing periods parameter is required
- Claiming no numeric columns
df with daily stock prices:import pandas as pd
df = pd.DataFrame({'Price': [50, 52, 51, 53, 54]})You want to calculate the percentage change over 2 days instead of 1. Which code will give the correct result?
Solution
Step 1: Understand the periods parameter
Theperiodsparameter inpct_change()sets how many rows to look back for percentage change calculation. For 2-day change, useperiods=2.Step 2: Check other options
df.pct_change(periods=1) calculates 1-day change, df.pct_change(axis=1) uses axis=1 which is incorrect for this data, and df.pct_change(periods=0) computes zero change.Final Answer:
df.pct_change(periods=2) -> Option CQuick Check:
periods=2 for 2-day change [OK]
- Using default periods=1 for 2-day change
- Using axis=1 incorrectly
- Setting periods=0 which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
