Identifying missing values (isnull, isna) in Data Analysis Python - Time & Space Complexity
We want to understand how long it takes to find missing values in data.
How does the time grow when the data gets bigger?
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]
})
missing = data.isnull()
This code creates a small table and checks which values are missing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each cell in the table to see if it is missing.
- How many times: Once for every cell in the data.
As the table gets bigger, the number of checks grows with the number of cells.
| Input Size (n cells) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of cells.
Time Complexity: O(n)
This means the time to find missing values grows in a straight line with the data size.
[X] Wrong: "Checking for missing values is instant no matter the data size."
[OK] Correct: Each cell must be checked, so bigger data takes more time.
Knowing how missing value checks scale helps you understand data cleaning speed in real projects.
"What if we check missing values only in one column instead of the whole table? How would the time complexity change?"
Practice
isnull() and isna() functions do in pandas?Solution
Step 1: Understand the purpose of
Both functions check for missing data and return True where data is missing.isnull()andisna()Step 2: Differentiate from other functions
They do not fill, remove, or convert data but only detect missing values.Final Answer:
They identify missing or empty values in a DataFrame or Series. -> Option AQuick Check:
Missing data detection = C [OK]
- Confusing detection with filling missing values
- Thinking they remove data instead of marking it
- Assuming they change data types
df?Solution
Step 1: Recall pandas function names for missing data
The correct pandas function to detect missing values isisnull().Step 2: Verify other options
Functions likechecknull(),missing(), andisempty()do not exist in pandas for this purpose.Final Answer:
df.isnull() -> Option CQuick Check:
Correct pandas method = D [OK]
- Using non-existent function names
- Confusing with methods that fill or drop data
- Misspelling function names
import pandas as pd
data = {'A': [1, None, 3], 'B': [4, 5, None]}
df = pd.DataFrame(data)
print(df.isna())Solution
Step 1: Understand the input DataFrame
The DataFrame has missing values at df['A'][1] and df['B'][2].Step 2: Apply
This method returns True at missing positions and False elsewhere.isna()methodFinal Answer:
A DataFrame showing True where values are missing and False otherwise. -> Option BQuick Check:
Missing spots = True, others = False [OK]
- Thinking isna() fills missing values
- Expecting all True or all False output
- Confusing isna() with dropna()
import pandas as pd
data = {'X': [10, 20, None]}
df = pd.DataFrame(data)
print(df.isnull)Solution
Step 1: Check the usage of isnull
isnull is a function and needs parentheses to execute, likedf.isnull().Step 2: Verify other parts of the code
DataFrame creation and variable names are correct; isnull exists in pandas.Final Answer:
The code is missing parentheses after isnull to call the function. -> Option DQuick Check:
Function call needs () = A [OK]
- Forgetting parentheses on function calls
- Assuming isnull is an attribute, not a method
- Misnaming variables or functions
df with columns ['Name', 'Age', 'Score']. Some Age and Score values are missing. How do you create a new column 'MissingCount' that counts how many missing values each row has?Solution
Step 1: Use
This returns True/False for each cell indicating missing data.isnull()to detect missing valuesStep 2: Sum missing values per row with
Summing True values (counted as 1) across columns gives missing count per row.sum(axis=1)Final Answer:
df['MissingCount'] = df.isnull().sum(axis=1) -> Option AQuick Check:
Sum True per row = B [OK]
- Summing without axis=1 sums all missing in DataFrame
- Using count() instead of sum() to count missing
- Using mean() which gives fraction, not count
Ready to prove it?
Pass the test and this pattern moves unknown
