0
0
Pandasdata~10 mins

dt accessor for datetime properties in Pandas - Interactive Code Practice

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

Complete the code to extract the year from the 'date' column using the dt accessor.

Pandas
df['year'] = df['date'].dt.[1]
Drag options to blanks, or click blank then click option'
Aday
Bmonth
Chour
Dyear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'month' or 'day' instead of 'year'.
Forgetting to use the dt accessor.
2fill in blank
medium

Complete the code to extract the month from the 'date' column using the dt accessor.

Pandas
df['month'] = df['date'].dt.[1]
Drag options to blanks, or click blank then click option'
Amonth
Byear
Cminute
Dday
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' or 'year' instead of 'month'.
Forgetting to use the dt accessor.
3fill in blank
hard

Fix the error in the code to extract the day from the 'date' column using the dt accessor.

Pandas
df['day'] = df['date'].[1]
Drag options to blanks, or click blank then click option'
Adt.day
Bday
Cday.dt
Ddt.day()
Attempts:
3 left
💡 Hint
Common Mistakes
Using df['date'].day without dt accessor.
Using parentheses like dt.day() which is incorrect.
4fill in blank
hard

Fill both blanks to create a new column 'hour' extracting the hour from 'timestamp' column and filter rows where hour is greater than 12.

Pandas
df['hour'] = df['timestamp'].[1].hour
filtered = df[df['hour'] [2] 12]
Drag options to blanks, or click blank then click option'
Adt
B>
C<
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'date' instead of 'dt' for accessor.
Using '<' instead of '>' in filter condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as day names in uppercase, values as day numbers, filtering days greater than 15 from 'date' column.

Pandas
result = dict(zip([1], [2]))
result = {k: v for k, v in result.items() if v [3] 15}
result = {k.upper(): v for k, v in result.items()}
Drag options to blanks, or click blank then click option'
Adf['date'].dt.month_name()
Bdf['date'].dt.day
C>
Ddf['date'].dt.day_name()
Attempts:
3 left
💡 Hint
Common Mistakes
Using month_name() instead of day_name() for keys.
Using '<' instead of '>' in filtering.
Not converting keys to uppercase.