logo

Extracting Date Components

Once you have a datetime column, extract components using .dt:

df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day

Get the day of week (Monday=0):

df['weekday'] = df['date'].dt.dayofweek
df['day_name'] = df['date'].dt.day_name()

Extract time parts:

df['hour'] = df['date'].dt.hour
df['minute'] = df['date'].dt.minute

These components enable time-based grouping - sales by month, traffic by hour, patterns by day of week.

For time series analysis, see The Ultimate Pandas Bootcamp.