C. Cannot convert float to int directly if float has decimals
D. astype() cannot be chained
Solution
Step 1: Examine the code snippet
The code uses pd.DataFrame but no import statement is shown.
Step 2: Identify the runtime error
Without import pandas as pd, pd raises NameError: name 'pd' is not defined.
Final Answer:
DataFrame missing import statement -> Option A
Quick Check:
Missing import causes NameError [OK]
Hint: Always import pandas as pd before using pd.DataFrame [OK]
Common Mistakes:
Forgetting the import pandas as pd statement
Assuming pd is predefined
Focusing on dtype conversions instead of basic syntax
5. You have a DataFrame with a column 'date' containing strings like '2023-01-01'. You want to analyze by month but get errors when grouping. What is the best way to fix the dtype before grouping?
hard
A. Convert 'date' column to integer using astype(int)
B. Convert 'date' column to datetime using pd.to_datetime()
C. Leave 'date' as string and group directly
D. Drop the 'date' column before grouping
Solution
Step 1: Identify the problem with string dates
Grouping by string dates does not allow easy extraction of months.
Step 2: Convert strings to datetime type
Use pd.to_datetime() to convert strings to datetime objects.
Step 3: Group by month after conversion
Datetime dtype allows grouping by month using dt.month.
Final Answer:
Convert 'date' column to datetime using pd.to_datetime() -> Option B
Quick Check:
Datetime dtype needed for date grouping [OK]
Hint: Use pd.to_datetime() to fix date strings before grouping [OK]
Common Mistakes:
Trying to group by string dates directly
Converting dates to int which loses meaning
Dropping date column instead of fixing dtype
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.