0
0
ML Pythonml~10 mins

Date and time feature extraction in ML Python - 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 a datetime column in pandas.

ML Python
df['year'] = df['date_column'].dt.[1]
Drag options to blanks, or click blank then click option'
Aday
Byear
Chour
Dmonth
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'month' or 'day' instead of 'year'.
Forgetting to use the .dt accessor before the attribute.
2fill in blank
medium

Complete the code to extract the weekday name from a datetime column in pandas.

ML Python
df['weekday'] = df['date_column'].dt.[1]()
Drag options to blanks, or click blank then click option'
Aday_name
Bweekday_name
Cday
Dweekday
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'weekday' which returns an integer instead of the name.
Using 'weekday_name' which is deprecated.
3fill in blank
hard

Fix the error in the code to extract the hour from a datetime column.

ML Python
df['hour'] = df['date_column'].[1].hour
Drag options to blanks, or click blank then click option'
Atime
Bdatetime
Cdt
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access .hour directly without .dt.
Using 'datetime' or 'time' which are not pandas accessors.
4fill in blank
hard

Fill both blanks to create a new column with the month and day extracted from a datetime column.

ML Python
df['month_day'] = df['date_column'].dt.[1].astype(str) + '-' + df['date_column'].dt.[2].astype(str)
Drag options to blanks, or click blank then click option'
Amonth
Bday
Chour
Dyear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hour' or 'year' instead of 'month' or 'day'.
Not converting numbers to strings before concatenation.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each date to its weekday number if the hour is greater than 12.

ML Python
result = {date: date.[1] for date in df['date_column'] if date.[2] [3] 12}
Drag options to blanks, or click blank then click option'
Aweekday
Bhour
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the comparison.
Using 'day' instead of 'weekday' for the first blank.
Using 'date' or 'time' instead of 'dt' accessor (not shown here but common).