📖 Scenario: You work in a small company that keeps track of employee joining dates. You want to analyze the year, month, and day separately to find trends.
🎯 Goal: Build a small program that extracts the year, month, and day from a list of joining dates.
📋 What You'll Learn
Create a list of dates as strings in 'YYYY-MM-DD' format
Create a helper variable to store an empty list for years
Use a for loop to extract year, month, and day from each date string
Print the lists of years, months, and days
💡 Why This Matters
🌍 Real World
Extracting parts of dates helps businesses analyze trends like hiring patterns or sales by month.
💼 Career
Data analysts often need to break down dates to group and summarize data for reports.
Progress0 / 4 steps
1
Create the list of joining dates
Create a list called joining_dates with these exact date strings: '2023-01-15', '2022-12-05', '2021-07-23', '2023-03-30', '2020-11-11'.
Data Analysis Python
Hint
Use square brackets [] to create a list and put the date strings inside quotes separated by commas.
2
Create empty lists for year, month, and day
Create three empty lists called years, months, and days to store the extracted parts of the dates.
Data Analysis Python
Hint
Use empty square brackets [] to create empty lists.
3
Extract year, month, and day from each date
Use a for loop with variable date to go through joining_dates. Inside the loop, extract the year as the first 4 characters, the month as characters 5 to 7, and the day as characters 8 to 10 from date. Append the year to years, the month to months, and the day to days.
Data Analysis Python
Hint
Use string slicing like date[0:4] to get the year part.
4
Print the extracted year, month, and day lists
Print the lists years, months, and days each on a separate line.
Data Analysis Python
Hint
Use three separate print() statements to show each list.
Practice
(1/5)
1. What does the dt.year attribute do when applied to a pandas datetime column?
easy
A. Converts the dates to string format
B. Extracts the month part from each date in the column
C. Extracts the day part from each date in the column
D. Extracts the year part from each date in the column
Solution
Step 1: Understand pandas datetime attributes
The dt accessor allows extraction of date parts like year, month, day from datetime columns.
Step 2: Identify what dt.year extracts
dt.year specifically extracts the year component from each datetime value.
Final Answer:
Extracts the year part from each date in the column -> Option D
The dates are '2023-01-15', '2022-12-31', and '2024-07-04'. Extracting dt.month gets the month numbers: 1, 12, and 7.
Step 2: Convert the extracted months to list
The tolist() method converts the pandas Series to a Python list: [1, 12, 7].
Final Answer:
[1, 12, 7] -> Option A
Quick Check:
Months extracted = [1, 12, 7] [OK]
Hint: dt.month returns month numbers as integers [OK]
Common Mistakes:
Confusing day with month values
Expecting month names instead of numbers
Using dt.day instead of dt.month
4. The following code throws an error. What is the main issue?
import pandas as pd
dates = pd.Series(['2023-01-15', '2022-12-31'])
years = dates.dt.year
print(years)
medium
A. print statement syntax is incorrect
B. dt.year is not a valid attribute
C. dates is not converted to datetime type before using dt accessor
D. dates should be a list, not a pandas Series
Solution
Step 1: Check the data type of dates
The dates Series contains strings, not datetime objects.
Step 2: Understand dt accessor requirements
The dt accessor works only on datetime dtype Series. Without conversion, it raises an error.
Final Answer:
dates is not converted to datetime type before using dt accessor -> Option C
Quick Check:
Convert strings to datetime before dt.year [OK]
Hint: Always convert to datetime before using dt attributes [OK]
Common Mistakes:
Forgetting pd.to_datetime conversion
Assuming dt.year works on string Series
Misreading error as syntax problem
5. You have a DataFrame df with a datetime column 'order_date'. You want to add three new columns: 'year', 'month', and 'day' extracted from 'order_date'. Which code correctly does this?
hard
A. df['year'] = df['order_date'].year
df['month'] = df['order_date'].month
df['day'] = df['order_date'].day
B. df['year'] = df['order_date'].dt.year
df['month'] = df['order_date'].dt.month
df['day'] = df['order_date'].dt.day