0
0
Pandasdata~10 mins

Extracting day of week and hour in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extracting day of week and hour
Start with datetime column
Extract day of week
Extract hour
Add new columns to DataFrame
Use extracted info for analysis
We start with a datetime column, extract the day of week and hour, then add these as new columns for easier analysis.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2024-06-01 08:30', '2024-06-02 15:45'])})
df['day_of_week'] = df['date'].dt.day_name()
df['hour'] = df['date'].dt.hour
print(df)
This code creates a DataFrame with datetime values, extracts the day name and hour, and adds them as new columns.
Execution Table
StepActionInputOutput
1Create DataFrame with datetime['2024-06-01 08:30', '2024-06-02 15:45']DataFrame with 'date' column as datetime
2Extract day of weekdf['date']['Saturday', 'Sunday']
3Add 'day_of_week' column['Saturday', 'Sunday']DataFrame with 'day_of_week' column
4Extract hourdf['date'][8, 15]
5Add 'hour' column[8, 15]DataFrame with 'hour' column
6Print final DataFrameDataFrame with all columnsdate day_of_week hour 2024-06-01 08:30:00 Saturday 8 2024-06-02 15:45:00 Sunday 15
💡 All datetime values processed, new columns added successfully.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
dfemptydate column with datetimeadded day_of_week columnadded hour columnfinal DataFrame with date, day_of_week, hour
Key Moments - 2 Insights
Why do we use dt.day_name() instead of just day?
dt.day_name() gives the weekday name like 'Monday', 'Tuesday' which is easier to read, as shown in execution_table step 2 and 3.
What type of data is in the 'hour' column?
The 'hour' column contains integers representing the hour part of the datetime, as seen in execution_table step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of extracting day of week at step 2?
A[8, 15]
B['Monday', 'Tuesday']
C['Saturday', 'Sunday']
D['2024-06-01', '2024-06-02']
💡 Hint
Check the 'Output' column in execution_table row for step 2.
At which step is the 'hour' column added to the DataFrame?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the action 'Add hour column' in execution_table.
If the datetime was '2024-06-01 23:59', what would the 'hour' value be?
A23
B59
C0
D12
💡 Hint
Refer to how hour is extracted in execution_table step 4.
Concept Snapshot
Use pandas dt accessor to extract parts of datetime:
- day_of_week = df['date'].dt.day_name() gives weekday name
- hour = df['date'].dt.hour gives hour as integer
Add these as new columns for easy analysis.
Full Transcript
We start with a DataFrame containing datetime values. Using pandas dt accessor, we extract the day of week as a name string and the hour as an integer. These are added as new columns to the DataFrame. This helps us analyze data by weekday or hour easily. The execution table shows each step: creating the DataFrame, extracting day names, adding the column, extracting hours, adding that column, and printing the final DataFrame. Variables change as columns are added. Common confusions include why day_name() is used and what data type hour is. The quiz tests understanding of these steps and outputs.