0
0
Pandasdata~10 mins

Why datetime handling matters in Pandas - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 'date' column to datetime format.

Pandas
import pandas as pd

df = pd.DataFrame({'date': ['2023-01-01', '2023-02-01', '2023-03-01']})
df['date'] = pd.[1](df['date'])
Drag options to blanks, or click blank then click option'
Ato_datetime
Bto_string
Cto_numeric
Dto_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_string instead of to_datetime causes no date conversion.
Using to_numeric will cause an error for date strings.
2fill in blank
medium

Complete the code to extract the year from the 'date' column.

Pandas
df['year'] = df['date'].dt.[1]
Drag options to blanks, or click blank then click option'
Ayear
Bmonth
Cday
Dhour
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'month' or 'day' extracts the wrong part of the date.
Trying to call a method instead of accessing the attribute.
3fill in blank
hard

Fix the error in the code to filter rows where the date is after January 31, 2023.

Pandas
filtered = df[df['date'] > pd.[1]('2023-01-31')]
Drag options to blanks, or click blank then click option'
Ato_string
Bto_datetime
Cto_numeric
Dto_list
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing datetime column with a string causes wrong filtering.
Using to_string does not convert to datetime.
4fill in blank
hard

Fill both blanks to create a new column 'month_day' with month and day as strings.

Pandas
df['month_day'] = df['date'].dt.[1].astype(str) + '-' + df['date'].dt.[2].astype(str)
Drag options to blanks, or click blank then click option'
Amonth
Byear
Cday
Dhour
Attempts:
3 left
💡 Hint
Common Mistakes
Using year or hour instead of month or day.
Not converting to string before concatenation.
5fill in blank
hard

Fill all three blanks to create a dictionary with date as key and year as value for dates after 2023-01-15.

Pandas
result = {row['date']: row['date'].dt.[1] for _, row in df[df['date'] > pd.[2]('2023-01-15')].iterrows() if row['date'].dt.[3] == 2023}
Drag options to blanks, or click blank then click option'
Ayear
Bto_datetime
Dmonth
Attempts:
3 left
💡 Hint
Common Mistakes
Using month instead of year for filtering.
Not converting the string date to datetime before comparison.