info() for column types and nulls in Pandas - Time & Space Complexity
We want to understand how long it takes for pandas to show column types and count missing values using info().
How does the time grow when the data has more rows or columns?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 1000
df = pd.DataFrame({
'A': range(n),
'B': [None if i % 10 == 0 else i for i in range(n)],
'C': ['text'] * n
})
df.info()
This code creates a DataFrame with n rows and 3 columns, then calls info() to show data types and count nulls.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: pandas scans each column to count non-null values and check data types.
- How many times: It processes each of the
nrows once per column.
As the number of rows grows, pandas must check more data to count nulls and types.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 checks (3 columns x 10 rows) |
| 100 | About 300 checks |
| 1000 | About 3000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows times columns.
Time Complexity: O(n x m)
This means the time grows linearly with the number of rows n and columns m.
[X] Wrong: "Calling info() is always very fast and does not depend on data size."
[OK] Correct: Actually, info() looks at every row in each column to count nulls and types, so bigger data means more work and longer time.
Knowing how info() scales helps you understand data inspection costs and prepares you to explain performance in real data projects.
What if we added many more columns instead of rows? How would the time complexity change?
Practice
info() method in pandas primarily show about a DataFrame?Solution
Step 1: Understand what
Theinfo()displaysinfo()method shows the count of non-null entries and the data type for each column in a DataFrame.Step 2: Compare with other options
The first five rows are shown byhead(), summary statistics like mean and median bydescribe(), and unique values by methods likeunique(), notinfo().Final Answer:
The number of non-null values and data types of each column -> Option AQuick Check:
info()= non-null counts + dtypes [OK]
- Confusing info() with head() or describe()
- Thinking info() shows unique values
- Assuming info() shows data content
info() method on a pandas DataFrame named df?Solution
Step 1: Recall method call syntax in pandas
Methods are called with parentheses after the method name, sodf.info()is correct.Step 2: Check other options for syntax errors
info(df) treats info as a function, which it is not. df.info misses parentheses, so it returns the method object, not the output. df.info[] uses invalid syntax with brackets.Final Answer:
df.info() -> Option AQuick Check:
Method call needs parentheses: df.info() [OK]
- Forgetting parentheses when calling methods
- Using function syntax instead of method syntax
- Using brackets [] instead of parentheses ()
df below, what will df.info() show for the column Age?
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, None, 30]}
df = pd.DataFrame(data)
df.info()Solution
Step 1: Count non-null values in Age column
The Age column has values 25, None, and 30. None counts as null, so non-null count is 2.Step 2: Determine data type of Age column
Because of the None, pandas converts the Age column to float64 to accommodate NaN values.Final Answer:
Age column has 2 non-null values and dtype float64 -> Option CQuick Check:
Nulls reduce count; None makes dtype float64 [OK]
- Counting None as non-null
- Assuming dtype stays int64 despite nulls
- Ignoring dtype conversion due to nulls
df.info() but get an error: AttributeError: 'DataFrame' object has no attribute 'info'. What is the most likely cause?Solution
Step 1: Understand the error meaning
The error saysdfhas no attributeinfo, meaningdfis not a DataFrame.Step 2: Identify common cause
This usually happens ifdfwas reassigned to something else, like a list or string, losing DataFrame methods.Final Answer:
You overwrote the variable namedfwith a non-DataFrame object -> Option DQuick Check:
AttributeError means wrong object type [OK]
- Assuming empty DataFrame causes this error
- Thinking missing parentheses cause AttributeError
- Ignoring variable reassignment issues
df with columns ['A', 'B', 'C']. Column B has many null values. You want to quickly check which columns have missing data and their types. Which code snippet best helps you decide what cleaning is needed?Solution
Step 1: Identify what each method shows
df.info()shows non-null counts and data types, helping spot missing data and column types.Step 2: Compare with other options
df.describe()summarizes numeric stats but not null counts.df.isnull().sum()shows null counts but not types.df.head()shows sample rows but not summary info.Final Answer:
df.info() -> Option BQuick Check:
info() = null counts + types [OK]
- Using describe() expecting null info
- Using isnull().sum() but ignoring types
- Using head() which shows no summary
Ready to prove it?
Pass the test and this pattern moves unknown
