Date and Time-Based Features
Swipe to show menu
Extracting calendar-based features from date and time information is a powerful technique in temporal feature engineering. By breaking down a timestamp into components such as the day of the week, month, or whether it falls on a weekend, you can capture recurring patterns or seasonality that often drive changes in real-world data. These features are especially valuable in time series forecasting, where many phenomena – like sales, traffic, or energy usage – follow predictable cycles tied to the calendar. For example, demand might spike on weekends or drop during certain months, and models that incorporate this information can make more accurate predictions.
12345678910111213141516import pandas as pd # Create a sample datetime index dates = pd.date_range(start="2024-06-01", end="2024-06-14", freq="D") df = pd.DataFrame(index=dates) # Extract day of week (Monday=0, Sunday=6) df["day_of_week"] = df.index.dayofweek # Extract month df["month"] = df.index.month # Determine if the date is a weekend (Saturday=5, Sunday=6) df["is_weekend"] = df.index.dayofweek >= 5 print(df)
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat