📖 Scenario: You work at a delivery company. You have a list of delivery times. You want to find out which day of the week and hour of the day most deliveries happen.
🎯 Goal: Create a pandas DataFrame with delivery timestamps. Then extract the day of the week and hour from these timestamps. Finally, display the new data.
📋 What You'll Learn
Create a pandas DataFrame called df with a column 'delivery_time' containing the exact timestamps given.
Create a variable called time_column and set it to the string 'delivery_time'.
Use pandas datetime properties to create two new columns in df: 'day_of_week' and 'hour', extracted from time_column.
Print the updated DataFrame df.
💡 Why This Matters
🌍 Real World
Delivery companies analyze delivery times to optimize routes and staffing. Extracting day and hour helps find busy times.
💼 Career
Data analysts and scientists often work with timestamps to find patterns in time-based data.
Progress0 / 4 steps
1
Create the DataFrame with delivery timestamps
Create a pandas DataFrame called df with one column named 'delivery_time'. The column should contain these exact timestamps as strings: '2024-06-01 08:30:00', '2024-06-02 14:45:00', '2024-06-03 19:00:00', '2024-06-04 09:15:00', and '2024-06-05 22:10:00'.
Pandas
Hint
Use pd.DataFrame and pass a dictionary with the key 'delivery_time' and the list of timestamps as values.
2
Set the time column variable
Create a variable called time_column and set it to the string 'delivery_time'.
Pandas
Hint
Just assign the string 'delivery_time' to the variable time_column.
3
Extract day of week and hour from timestamps
Convert the time_column in df to datetime type. Then create two new columns in df: 'day_of_week' which holds the day of the week as an integer (Monday=0), and 'hour' which holds the hour of the day as an integer.
Pandas
Hint
Use pd.to_datetime() to convert the column. Then use .dt.dayofweek and .dt.hour to get the new columns.
4
Print the updated DataFrame
Print the DataFrame df to see the new columns 'day_of_week' and 'hour'.
Pandas
Hint
Use print(df) to show the DataFrame with the new columns.
Practice
(1/5)
1. What does the .dt.dayofweek attribute return when applied to a pandas datetime column?
easy
A. An integer from 0 (Monday) to 6 (Sunday) representing the day of the week
B. The name of the day as a string like 'Monday' or 'Tuesday'
C. The hour of the day from 0 to 23
D. The month number from 1 to 12
Solution
Step 1: Understand the .dt accessor in pandas
The .dt accessor allows extraction of datetime properties from a pandas datetime column.
Step 2: Identify what .dt.dayofweek returns
.dt.dayofweek returns an integer where Monday is 0 and Sunday is 6, representing the day of the week.
Final Answer:
An integer from 0 (Monday) to 6 (Sunday) representing the day of the week -> Option A
Quick Check:
.dt.dayofweek = 0 to 6 [OK]
Hint: Remember Monday starts at 0 for dayofweek [OK]
Common Mistakes:
Thinking it returns day names instead of numbers
Confusing dayofweek with day or dayofyear
Expecting Sunday to be 0 instead of Monday
2. Which of the following is the correct way to extract the hour from a pandas datetime column named df['time']?
easy
A. df['time'].hour
B. df['time'].dt.hour()
C. df['time'].dt.hour
D. df['time'].dt['hour']
Solution
Step 1: Recall the pandas datetime accessor syntax
To extract datetime components, use the .dt accessor followed by the attribute without parentheses.
Step 2: Identify the correct syntax for hour extraction
The correct syntax is df['time'].dt.hour, which returns the hour as an integer from 0 to 23.
Final Answer:
df['time'].dt.hour -> Option C
Quick Check:
Use .dt.hour without parentheses [OK]
Hint: Use .dt.hour without parentheses to get hour [OK]
B. Incorrect use of .hour instead of .dt.hour with parentheses
C. The 'time' column should be a list of integers, not strings
D. Missing conversion of 'time' column to datetime before accessing .hour
Solution
Step 1: Check the data type of 'time' column
The 'time' column contains strings, not datetime objects, so .hour attribute is not available.
Step 2: Identify the fix
Convert 'time' to datetime using pd.to_datetime() before accessing .dt.hour.
Final Answer:
Missing conversion of 'time' column to datetime before accessing .hour -> Option D
Quick Check:
Convert strings to datetime first [OK]
Hint: Always convert to datetime before .dt.hour [OK]
Common Mistakes:
Trying to use .hour on string columns
Using .hour without .dt accessor
Ignoring data type conversion errors
5. You have a pandas DataFrame df with a column 'datetime' containing timestamps. You want to create a new column 'work_hour' that is True if the timestamp is on a weekday (Monday to Friday) and between 9 AM and 5 PM, otherwise False. Which code snippet correctly does this?