Detecting missing values with isna() in Pandas - Time & Space Complexity
We want to understand how the time to find missing values changes as data grows.
How does checking for missing data scale with more rows?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.DataFrame({
'A': [1, 2, None, 4],
'B': [None, 2, 3, 4],
'C': [1, None, None, 4]
})
missing_mask = data.isna()
This code creates a table and checks each cell to see if it is missing (NaN).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each cell in the DataFrame for missing value.
- How many times: Once per cell, so number of rows times number of columns.
As the table gets bigger, the number of checks grows with total cells.
| Input Size (rows x columns) | Approx. Operations |
|---|---|
| 10 x 5 = 50 | 50 checks |
| 100 x 5 = 500 | 500 checks |
| 1000 x 5 = 5000 | 5000 checks |
Pattern observation: The work grows directly with the number of cells.
Time Complexity: O(n * m)
This means the time grows proportionally with the number of rows (n) times columns (m).
[X] Wrong: "Checking for missing values is constant time regardless of data size."
[OK] Correct: Each cell must be checked, so more data means more work.
Understanding how data size affects missing value detection helps you reason about data cleaning speed in real projects.
"What if we only check one column for missing values instead of the whole DataFrame? How would the time complexity change?"
Practice
pandas.DataFrame.isna() function do?Solution
Step 1: Understand the purpose of
Theisna()isna()function checks each cell in a DataFrame and marks True if the value is missing (NaN), otherwise False.Step 2: Compare options with function behavior
Only It shows which values in the DataFrame are missing (NaN). correctly describes this behavior. Options B, C, and D describe other functions or actions.Final Answer:
It shows which values in the DataFrame are missing (NaN). -> Option DQuick Check:
isna()= marks missing values True [OK]
- Confusing isna() with fillna()
- Thinking isna() removes data
- Assuming isna() changes data types
df?Solution
Step 1: Recall method call syntax in pandas
Methods likeisna()must be called with parentheses on the DataFrame object, sodf.isna()is correct.Step 2: Evaluate each option
df.isna() uses correct syntax. df.isna misses parentheses, so it returns the method object, not the result. isna(df) is incorrect becauseisnais a method, not a standalone function. df.isna[] uses invalid bracket syntax.Final Answer:
df.isna() -> Option CQuick Check:
Method call needs parentheses () [OK]
- Forgetting parentheses after isna
- Trying to call isna as a standalone function
- Using brackets instead of parentheses
import pandas as pd
data = {'A': [1, None, 3], 'B': [4, 5, None]}
df = pd.DataFrame(data)
print(df.isna())What is the output?
Solution
Step 1: Identify missing values in each column
Column 'A' has None at index 1, so True there. Column 'B' has None at index 2, so True there. All other values are present (False).Step 2: Match the True/False pattern to output
A B 0 False False 1 True False 2 False True matches the pattern: index 1 in 'A' is True, index 2 in 'B' is True, others False.Final Answer:
A B 0 False False 1 True False 2 False True -> Option AQuick Check:
None values marked True by isna() [OK]
- Mixing row and column positions
- Confusing True and False values
- Assuming isna() replaces values
import pandas as pd
data = {'X': [10, 20, None], 'Y': [None, 40, 50]}
df = pd.DataFrame(data)
missing = df.isnaWhy does
missing.sum() cause an error?Solution
Step 1: Understand method assignment without parentheses
Assigningmissing = df.isnastores the method itself, not the result. To get the result, you must call it with parentheses.Step 2: Explain why
Assigningmissing.sum()causes errormissing = df.isnastores the method object, not the DataFrame result. Method objects do not have a.sum()attribute--only DataFrames do. Thus,missing.sum()raises AttributeError.Final Answer:
Becauseisnais not called with parentheses when assigned. -> Option BQuick Check:
Assign method with () to get result [OK]
- Assigning method without parentheses and expecting result
- Confusing method object with method call
- Assuming isna() is not a pandas method
df with columns 'Age' and 'Salary'. You want to count how many missing values are in each column using isna(). Which code correctly does this?Solution
Step 1: Use
Theisna()to mark missing valuesisna()method returns True for missing values and False otherwise.Step 2: Sum True values per column to count missing
Since True is treated as 1 and False as 0, summing over rows counts missing values per column. Sodf.isna().sum()gives the count per column.Final Answer:
df.isna().sum() -> Option AQuick Check:
Sum True from isna() to count missing [OK]
- Forgetting parentheses after isna
- Using count() which counts all rows, not missing
- Applying sum() before isna()
Ready to prove it?
Pass the test and this pattern moves unknown
