Data validation checks in Pandas - Time & Space Complexity
When we check data for errors or missing values using pandas, we want to know how long these checks take as data grows.
We ask: How does the time to validate data change when the data size increases?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'age': [25, 30, None, 22, 40],
'salary': [50000, 60000, 55000, None, 70000]
})
missing_age = df['age'].isnull().sum()
valid_salary = (df['salary'] > 0).all()
This code checks how many missing values are in the 'age' column and verifies if all 'salary' values are positive.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each value in the 'age' and 'salary' columns once.
- How many times: Once per column, so twice total, each over all rows.
As the number of rows grows, the time to check missing or positive values grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 checks (2 columns x 10 rows) |
| 100 | About 200 checks |
| 1000 | About 2000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to validate data grows in a straight line as the data size grows.
[X] Wrong: "Checking for missing values is instant no matter how big the data is."
[OK] Correct: Each value must be checked once, so more data means more time.
Understanding how data checks scale helps you write efficient code and explain your choices clearly in real projects.
"What if we checked multiple columns instead of two? How would the time complexity change?"
Practice
isnull() check in a DataFrame?Solution
Step 1: Understand the purpose of
Theisnull()isnull()function in pandas returns a DataFrame of the same shape with True where values are missing (NaN) and False otherwise.Step 2: Compare with other functions
Functions likeduplicated()check duplicates, anddtypesshows data types, so they do not check for missing values.Final Answer:
It checks for missing or null values in the data. -> Option AQuick Check:
Missing data = isnull() [OK]
- Confusing isnull() with duplicated()
- Thinking isnull() returns data types
- Assuming isnull() sorts data
df?Solution
Step 1: Recall pandas method for duplicates
The correct pandas method to find duplicate rows isduplicated(), called asdf.duplicated().Step 2: Verify other options
isnull()checks missing data, andcheck_duplicates()andfind_duplicates()are not valid pandas methods.Final Answer:
df.duplicated() -> Option CQuick Check:
Duplicates = duplicated() [OK]
- Using non-existent methods like check_duplicates()
- Mixing isnull() with duplicate checks
- Forgetting parentheses after duplicated
df below, what will df.isnull().sum() output?import pandas as pd
data = {'A': [1, 2, None], 'B': [None, 2, 3], 'C': [1, 2, 3]}
df = pd.DataFrame(data)Solution
Step 1: Identify missing values in each column
Column 'A' has one None (missing), 'B' has one None, and 'C' has no missing values.Step 2: Understand what
isnull().sum()doesisnull()marks missing as True, andsum()counts True values per column.Final Answer:
{'A': 1, 'B': 1, 'C': 0} -> Option DQuick Check:
Count missing per column = isnull().sum() [OK]
- Counting total missing instead of per column
- Confusing None with zero
- Misreading DataFrame values
df. What is the error?duplicates = df.duplicate()
Solution
Step 1: Check method name correctness
The correct pandas method to find duplicates isduplicated(), notduplicate().Step 2: Verify other options
dfis assumed defined,duplicated()does not require arguments, andisnull()checks missing data, not duplicates.Final Answer:
The method name should be duplicated(), not duplicate(). -> Option AQuick Check:
Correct method name = duplicated() [OK]
- Misspelling duplicated() as duplicate()
- Confusing duplicated() with isnull()
- Thinking duplicated() needs arguments
df with columns ['id', 'age', 'score']. You want to validate that age has no missing values and score has no duplicates. Which code correctly performs both checks?Solution
Step 1: Check missing values in 'age'
df['age'].isnull().sum()counts missing values correctly.missing_age = df['age'].isnull().sum() duplicate_score = df['score'].duplicated().any()
uses this.Step 2: Check duplicates in 'score'
df['score'].duplicated().any()returns True if any duplicates exist, which is a good validation check.Step 3: Review other options
missing_age = df.isnull().sum()['age'] duplicate_score = df['score'].isnull().any()
wrongly checks duplicates with isnull(),missing_age = df['age'].duplicated().sum() duplicate_score = df['score'].duplicated().sum()
uses duplicated() on 'age' which is wrong,missing_age = df['age'].duplicated().any() duplicate_score = df['score'].isnull().sum()
wrongly uses duplicated() for missing and isnull() for duplicates.Final Answer:
missing_age = df['age'].isnull().sum(); duplicate_score = df['score'].duplicated().any() -> Option BQuick Check:
Missing count + duplicate check = correct validation [OK]
- Using duplicated() to count missing values
- Using isnull() to check duplicates
- Mixing sum() and any() incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
