📖 Scenario: You work in a retail company. You have a list of sales dates. You want to learn more about these dates by extracting useful parts like year, month, and day.
🎯 Goal: Build a small program that extracts the year, month, and day from a list of sales dates.
📋 What You'll Learn
Create a list of date strings in 'YYYY-MM-DD' format
Create a variable to hold the date format string
Use a loop to convert each date string to a date object and extract year, month, and day
Print the extracted features as a list of dictionaries
💡 Why This Matters
🌍 Real World
Extracting date features helps businesses analyze sales trends by month, day, or year.
💼 Career
Data analysts and scientists often need to break down dates to find patterns and make reports.
Progress0 / 4 steps
1
Create a list of sales dates
Create a list called sales_dates with these exact date strings: '2023-01-15', '2023-02-20', '2023-03-10', '2023-04-05', '2023-05-25'.
Data Analysis Python
Hint
Use square brackets to create a list. Put each date string inside quotes and separate by commas.
2
Create a date format string
Create a variable called date_format and set it to the string '%Y-%m-%d' to match the sales date format.
Data Analysis Python
Hint
This string tells Python how to read the date parts: year, month, day.
3
Extract year, month, and day from dates
Import datetime from the datetime module. Create an empty list called date_features. Use a for loop with variable date_str to go through sales_dates. Inside the loop, convert date_str to a date object using datetime.strptime(date_str, date_format). Then extract the year, month, and day from the date object and add a dictionary with keys 'year', 'month', and 'day' to date_features.
Data Analysis Python
Hint
Use datetime.strptime() to convert string to date. Use date_obj.year, date_obj.month, and date_obj.day to get parts.
4
Print the extracted date features
Write a print() statement to display the date_features list.
Data Analysis Python
Hint
Use print(date_features) to show the list of dictionaries.
Practice
(1/5)
1. What does the dt.year attribute do when applied to a pandas datetime column?
easy
A. Extracts the year part from each date in the column
B. Extracts the day of the week as a number
C. Converts the date to a string format
D. Calculates the difference between dates
Solution
Step 1: Understand pandas datetime accessor
The dt accessor allows extraction of date parts from datetime columns.
Step 2: Identify what dt.year extracts
dt.year extracts the year component from each datetime value.
Final Answer:
Extracts the year part from each date in the column -> Option A
Quick Check:
dt.year = year extraction [OK]
Hint: Remember dt.year gives the year number from dates [OK]
Common Mistakes:
Confusing dt.year with dt.day or dt.month
Thinking dt.year converts dates to strings
Assuming dt.year calculates date differences
2. Which of the following is the correct syntax to extract the month from a pandas datetime column named df['date']?
easy
A. df['date'].month
B. df['date'].dt.month
C. df['date'].get_month()
D. df['date'].extract('month')
Solution
Step 1: Recall pandas datetime accessor usage
To extract date parts, pandas requires the dt accessor before the attribute.
Step 2: Identify correct syntax for month extraction
The correct syntax is df['date'].dt.month. Other options are invalid methods or missing dt.
Final Answer:
df['date'].dt.month -> Option B
Quick Check:
Use dt.month to get month [OK]
Hint: Use .dt before date parts like month or day [OK]
Common Mistakes:
Omitting .dt accessor
Using non-existent methods like get_month()
Trying to extract with extract('month')
3. Given the following code, what will be the output?
Using dt.dayofweek, Monday=0 ... Sunday=6. Dates: 2023-01-15 is Sunday (6), 2023-07-04 is Tuesday (1), 2023-12-31 is Sunday (6).
Step 2: List the dayofweek values
The list is [6, 1, 6].
Final Answer:
[6, 1, 6] -> Option D
Quick Check:
Sunday=6, Tuesday=1, Sunday=6 [OK]
Hint: Remember Monday=0, Sunday=6 for dayofweek [OK]
Common Mistakes:
Confusing dayofweek with weekday names
Assuming Sunday=0 instead of 6
Mixing up dayofweek and day_name()
4. What is wrong with this code snippet to extract the hour from a datetime column df['timestamp']?
df['hour'] = df['timestamp'].hour
medium
A. Missing .dt accessor before .hour
B. The column must be converted to string first
C. The .hour attribute does not exist in pandas
D. The code should use .time.hour instead
Solution
Step 1: Check pandas datetime attribute access
Datetime properties require the dt accessor to access parts like hour.
Step 2: Identify missing .dt in code
The code uses df['timestamp'].hour which is invalid; it should be df['timestamp'].dt.hour.
Final Answer:
Missing .dt accessor before .hour -> Option A
Quick Check:
Use .dt.hour to extract hour [OK]
Hint: Always use .dt before date parts like hour [OK]
Common Mistakes:
Trying to access .hour directly on Series
Converting datetime to string unnecessarily
Using .time.hour which is invalid in pandas
5. You have a pandas DataFrame df with a datetime column 'order_date'. You want to create a new column 'is_weekend' that is True if the order was placed on Saturday or Sunday, and False otherwise. Which code snippet correctly achieves this?
hard
A. df['is_weekend'] = df['order_date'].dt.dayofweek == 6 or 5
B. df['is_weekend'] = df['order_date'].dt.weekday in [5,6]
C. df['is_weekend'] = df['order_date'].dt.dayofweek >= 5
D. df['is_weekend'] = df['order_date'].dt.day_name() in ['Saturday', 'Sunday']
Solution
Step 1: Understand dayofweek values for weekend
In pandas, dt.dayofweek returns 0 for Monday through 6 for Sunday. Weekend days are 5 (Saturday) and 6 (Sunday).
Step 2: Check which code correctly marks weekend
df['is_weekend'] = df['order_date'].dt.dayofweek >= 5 uses df['order_date'].dt.dayofweek >= 5, which is True for 5 and 6, correctly marking weekends. Other options use invalid syntax or incorrect logic.
Final Answer:
df['is_weekend'] = df['order_date'].dt.dayofweek >= 5 -> Option C
Quick Check:
Weekend days are dayofweek >= 5 [OK]
Hint: Weekend days have dayofweek 5 or 6, use >= 5 [OK]
Common Mistakes:
Using 'in' without vectorized methods
Using 'or' instead of bitwise operators
Confusing dayofweek numbers or names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.