0
0
Pandasdata~5 mins

str.lower() and str.upper() in Pandas - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: str.lower() and str.upper()
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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).
How Execution Grows With Input

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
1010 string conversions
100100 string conversions
10001000 string conversions

Pattern observation: The time grows in a straight line as the number of strings increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to convert all strings grows directly with how many strings there are.

Common Mistake

[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.

Interview Connect

Understanding how string operations scale helps you explain your code choices clearly and shows you know how data size affects performance.

Self-Check

"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?"