0
0
ML Pythonml~5 mins

Date and time feature extraction in ML Python

Choose your learning style9 modes available
Introduction

Date and time feature extraction helps us turn dates and times into useful numbers or categories. This makes it easier for machine learning models to understand patterns related to time.

You want to predict sales based on the day of the week or month.
You want to analyze customer behavior by hour of the day.
You want to detect seasonal trends in data like weather or traffic.
You want to improve a model by adding features like weekend or holiday flags.
You want to group data by time periods like quarters or years.
Syntax
ML Python
import pandas as pd

# Convert column to datetime type
df['date_column'] = pd.to_datetime(df['date_column'])

# Extract features
df['year'] = df['date_column'].dt.year
df['month'] = df['date_column'].dt.month
df['day'] = df['date_column'].dt.day
df['hour'] = df['date_column'].dt.hour
df['weekday'] = df['date_column'].dt.weekday  # Monday=0, Sunday=6
df['is_weekend'] = df['weekday'] >= 5  # True if Saturday or Sunday

Make sure your date column is in datetime format before extracting features.

You can extract many parts like year, month, day, hour, minute, second, weekday, and more.

Examples
Extract year and month from a date column.
ML Python
df['date'] = pd.to_datetime(df['date'])
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
Extract hour and weekday number (0=Monday) from datetime.
ML Python
df['hour'] = df['date'].dt.hour
df['weekday'] = df['date'].dt.weekday
Create a new column that is True if the date is Saturday or Sunday.
ML Python
df['is_weekend'] = df['date'].dt.weekday >= 5
Sample Model

This program converts date strings to datetime and extracts year, month, day, hour, weekday, and weekend flag.

ML Python
import pandas as pd

# Sample data with date strings
data = {'date': ['2024-06-01 14:30:00', '2024-06-02 09:15:00', '2024-06-08 20:45:00']}
df = pd.DataFrame(data)

# Convert to datetime
df['date'] = pd.to_datetime(df['date'])

# Extract features
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
df['hour'] = df['date'].dt.hour
df['weekday'] = df['date'].dt.weekday
df['is_weekend'] = df['weekday'] >= 5

print(df)
OutputSuccess
Important Notes

Weekday numbers start at 0 for Monday and end at 6 for Sunday.

Boolean features like 'is_weekend' help models learn special patterns for weekends.

Always check your date format before conversion to avoid errors.

Summary

Date and time features turn raw dates into useful numbers for models.

Common features include year, month, day, hour, weekday, and weekend flags.

These features help models find patterns related to time and improve predictions.