shape for dimensions in Pandas - Time & Space Complexity
We want to understand how checking the size of data changes as the data grows.
Specifically, how long does it take to get the shape of a pandas DataFrame?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 10
data = pd.DataFrame({
'A': range(n),
'B': range(n)
})
rows, cols = data.shape
print(f"Rows: {rows}, Columns: {cols}")
This code creates a DataFrame with n rows and 2 columns, then gets its shape.
- Primary operation: Accessing the stored shape attribute of the DataFrame.
- How many times: Exactly once, no loops or repeated steps.
Getting the shape does not depend on the number of rows or columns.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same no matter how big the data is.
Time Complexity: O(1)
This means getting the shape is very fast and does not take longer as the data grows.
[X] Wrong: "Getting the shape takes longer if the DataFrame has more rows or columns."
[OK] Correct: The shape is stored as a simple pair of numbers, so accessing it is instant regardless of size.
Knowing that some operations are instant helps you focus on the parts of code that really slow down with big data.
"What if we tried to count the number of unique values in a column instead? How would the time complexity change?"
Practice
shape attribute of a pandas DataFrame tell you?Solution
Step 1: Understand what
Theshapereturnsshapeattribute gives the size of the DataFrame as a tuple showing rows and columns.Step 2: Compare options with this definition
Only The number of rows and columns as a tuple (rows, columns) correctly describesshapeas (rows, columns).Final Answer:
The number of rows and columns as a tuple (rows, columns) -> Option DQuick Check:
shape= (rows, columns) [OK]
- Confusing shape with data types
- Thinking shape shows data preview
- Mixing shape with summary statistics
df?Solution
Step 1: Recall how to access attributes in pandas
Theshapeis an attribute, not a method, so no parentheses are used.Step 2: Check each option
df.shape usesdf.shapecorrectly without parentheses. Others are incorrect syntax.Final Answer:
df.shape -> Option BQuick Check:
Use attribute without () for shape [OK]
- Adding parentheses like a function call
- Using non-existent methods like get_shape()
- Trying to call shape as a standalone function
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
print(df.shape)What will be the output?
Solution
Step 1: Understand the DataFrame structure
The dictionary has 2 keys (columns) and each key has 3 values (rows).Step 2: Determine the shape
So the DataFrame has 3 rows and 2 columns, shape is (3, 2).Final Answer:
(3, 2) -> Option AQuick Check:
Rows = 3, Columns = 2 [OK]
- Swapping rows and columns order
- Using list brackets instead of tuple
- Expecting an error from correct code
import pandas as pd
df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
print(df.shape())What is the problem?
Solution
Step 1: Identify how shape is accessed
shapeis an attribute, not a method, so it should not have parentheses.Step 2: Understand the error cause
Callingdf.shape()tries to call a tuple as a function, causing a TypeError.Final Answer:
Using parentheses with shape causes an error -> Option CQuick Check:
Access shape without () to avoid error [OK]
- Treating shape like a function
- Assuming empty DataFrame causes error
- Ignoring import errors
s = pd.Series([10, 20, 30, 40]). What will s.shape return?Solution
Step 1: Understand Series shape
A pandas Series is one-dimensional, so its shape is a tuple with one element: the number of items.Step 2: Check the length of the Series
There are 4 elements, so shape is (4,).Final Answer:
(4,) -> Option AQuick Check:
Series shape is single-element tuple (length,) [OK]
- Expecting a 2D shape tuple
- Returning just an integer instead of tuple
- Thinking shape causes error on Series
Ready to prove it?
Pass the test and this pattern moves unknown
