0
0
Data Analysis Pythondata~30 mins

Date feature extraction in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Date Feature Extraction
📖 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.