str.lower() and str.upper() in Pandas - Time & Space Complexity
We want to understand how long it takes to change text to all lowercase or uppercase in pandas.
How does the time needed grow when we have more text data?
Analyze the time complexity of the following code snippet.
import pandas as pd
# Create a DataFrame with a column of strings
df = pd.DataFrame({
'names': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'] * 1000
})
# Convert all names to lowercase
lower_names = df['names'].str.lower()
# Convert all names to uppercase
upper_names = df['names'].str.upper()
This code changes all the strings in the 'names' column to lowercase and uppercase.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying the lower() or upper() function to each string in the column.
- How many times: Once for each string in the column (n times, where n is the number of rows).
Each string is processed one by one, so if we have more strings, the work grows directly with that number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 string conversions |
| 100 | 100 string conversions |
| 1000 | 1000 string conversions |
Pattern observation: The time grows in a straight line as the number of strings increases.
Time Complexity: O(n)
This means the time to convert all strings grows directly with how many strings there are.
[X] Wrong: "Changing case is instant and does not depend on data size."
[OK] Correct: Each string must be checked and changed, so more strings mean more work and more time.
Understanding how string operations scale helps you explain your code choices clearly and shows you know how data size affects performance.
"What if we changed from using str.lower() on a column to applying a custom function that also changes case and adds extra text? How would the time complexity change?"