Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is date and time feature extraction in machine learning?
It is the process of taking raw date and time data and turning it into useful pieces of information (features) that a machine learning model can understand and use to make predictions.
Click to reveal answer
beginner
Name three common features extracted from a date-time value.
Year, month, day of the week, hour, minute, and second are common features extracted from date-time values.
Click to reveal answer
beginner
Why might extracting the day of the week be useful for a model?
Because some patterns depend on the day, like sales being higher on weekends or traffic being heavier on weekdays.
Click to reveal answer
intermediate
How can you extract the hour from a timestamp in Python using pandas?
Use pandas' dt accessor: df['timestamp'].dt.hour extracts the hour part from a datetime column.
Click to reveal answer
intermediate
What is the benefit of extracting cyclical features like hour or month as sine and cosine values?
It helps the model understand that these features repeat in cycles, for example, hour 23 is close to hour 0, which normal numbers don't show well.
Click to reveal answer
Which feature is NOT typically extracted from a datetime value?
ADay of the week
BHour of the day
CUser's age
DMonth
✗ Incorrect
User's age is not a direct feature from datetime values; it requires additional data.
Why do we sometimes convert hour or month into sine and cosine values?
ATo capture the cyclical nature of time
BTo make the data categorical
CTo reduce the number of features
DTo normalize the data between 0 and 1
✗ Incorrect
Sine and cosine transformations help models understand that time features repeat in cycles.
Which Python library is commonly used to extract date and time features?
ANumPy
Bpandas
Cmatplotlib
Dscikit-learn
✗ Incorrect
pandas has built-in datetime functions to extract features easily.
What does df['date'].dt.weekday return in pandas?
AThe day of the week as an integer (Monday=0)
BThe day of the month
CThe week number in the year
DThe month number
✗ Incorrect
It returns the day of the week as an integer where Monday is 0 and Sunday is 6.
Which feature might help predict traffic patterns best?
AYear
BMicrosecond
CSecond
DDay of the week
✗ Incorrect
Traffic often changes depending on the day of the week (weekdays vs weekends).
Explain how you would extract useful features from a timestamp for a machine learning model.
Think about breaking down the timestamp into parts and representing repeating patterns.
You got /7 concepts.
Why is it important to transform cyclical time features like hour or month before using them in models?
Consider how time repeats and how numbers alone might mislead the model.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a common feature extracted from a date to help machine learning models?
easy
A. Font size
B. Color
C. Month
D. Temperature
Solution
Step 1: Understand date features
Date features include parts of a date like year, month, day, hour, and weekday.
Step 2: Identify relevant feature
Among the options, only 'Month' is a part of a date and useful for models.
Final Answer:
Month -> Option C
Quick Check:
Date feature = Month [OK]
Hint: Pick the option that relates directly to date parts [OK]
Common Mistakes:
Choosing unrelated features like color or font size
Confusing date features with unrelated data
2. Which Python code correctly extracts the weekday from a pandas datetime column named 'date'?
easy
A. df['weekday'] = df['date'].dt.weekday
B. df['weekday'] = df['date'].weekday()
C. df['weekday'] = df['date'].weekday
D. df['weekday'] = df['date'].dt.weekday()
Solution
Step 1: Recall pandas datetime accessor
To extract weekday, use the .dt accessor followed by .weekday without parentheses.
Step 2: Check each option
df['weekday'] = df['date'].dt.weekday uses .dt.weekday correctly. df['weekday'] = df['date'].weekday() calls weekday() directly on the series, which is invalid. df['weekday'] = df['date'].weekday misses .dt. df['weekday'] = df['date'].dt.weekday() incorrectly uses parentheses after .weekday.
Final Answer:
df['weekday'] = df['date'].dt.weekday -> Option A
Quick Check:
Use .dt.weekday without parentheses [OK]
Hint: Use .dt.weekday without parentheses for pandas datetime [OK]
Hint: Check weekday numbers: 5=Saturday, 6=Sunday for weekend [OK]
Common Mistakes:
Assuming weekend is false for Saturday/Sunday
Mixing hour extraction with weekend logic
Misreading weekday numbers
4. The following code aims to add a 'month' feature from a datetime column but throws an error:
df['month'] = df['date'].month
What is the error and how to fix it?
medium
A. AttributeError because .month must be accessed via .dt; fix: df['date'].dt.month
B. SyntaxError due to missing parentheses; fix: df['date'].month()
C. TypeError because 'date' is not datetime; fix: convert to datetime first
D. No error; code is correct
Solution
Step 1: Understand pandas datetime access
Datetime properties like month must be accessed with .dt when working on a pandas Series.
Step 2: Identify error cause
Using df['date'].month tries to get 'month' attribute of the Series, causing AttributeError.
Step 3: Correct code
Use df['date'].dt.month to extract month correctly.
Final Answer:
AttributeError because .month must be accessed via .dt; fix: df['date'].dt.month -> Option A
Quick Check:
Use .dt.month for pandas datetime columns [OK]
Hint: Always use .dt before datetime properties on pandas Series [OK]
Common Mistakes:
Missing .dt accessor
Trying to call .month() as a method
Not converting column to datetime type
5. You have a dataset with a datetime column 'timestamp'. You want to create a feature that is 1 if the time is during business hours (9am to 5pm) on weekdays, else 0. Which code correctly creates this feature?