Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconData Engineering Foundations
Data Engineering Foundations

Chapter 9: Time Series Data: Special Considerations

9.3 Practical Exercises for Chapter 9

These exercises will help you practice creating and interpreting date/time features, lagged features, and rolling features in time series data. Each exercise builds on the techniques discussed in this chapter, allowing you to deepen your understanding of time series data manipulation and feature engineering.

Exercise 1: Extracting Date/Time Features

You are given a dataset with daily sales records. Your task is to:

  1. Convert the Date column to datetime format.
  2. Extract the YearMonthDay of the Week, and Quarter as separate features.
import pandas as pd

# Sample data with dates
data = {'Date': ['2022-01-15', '2022-02-10', '2022-03-20', '2022-04-15', '2022-05-25'],
        'Sales': [200, 220, 250, 210, 230]}
df = pd.DataFrame(data)

# Solution: Convert Date column to datetime and extract date/time features
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Quarter'] = df['Date'].dt.quarter

print("Dataset with extracted date/time features:")
print(df)

Exercise 2: Creating Lagged Features

Using the same dataset, create lagged features to represent sales from:

  1. The previous day (Sales_Lag1).
  2. Two days prior (Sales_Lag2).
# Solution: Create lagged features
df['Sales_Lag1'] = df['Sales'].shift(1)
df['Sales_Lag2'] = df['Sales'].shift(2)

print("Dataset with lagged features:")
print(df)

Exercise 3: Creating Rolling Features

Using the same dataset, calculate the following rolling statistics:

  1. 3-day rolling mean for the Sales column.
  2. 3-day rolling standard deviation for the Sales column.
# Solution: Create rolling mean and standard deviation
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()
df['RollingStd_3'] = df['Sales'].rolling(window=3).std()

print("Dataset with rolling features:")
print(df)

Exercise 4: Combining Date/Time, Lagged, and Rolling Features

In this exercise, you’ll create a combined dataset with both date/time features, lagged features, and rolling features. Using the previous dataset, complete the following tasks:

  1. Extract YearMonth, and Day of the Week.
  2. Create a Sales_Lag1 feature for the previous day’s sales.
  3. Calculate a 3-day rolling mean for the Sales column.
# Solution: Combine date/time, lagged, and rolling features
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Sales_Lag1'] = df['Sales'].shift(1)
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()

print("Combined dataset with date/time, lagged, and rolling features:")
print(df)

Exercise 5: Encoding Cyclical Features

For the Day of the Week feature in the previous dataset, apply sine and cosine encoding to capture its cyclical nature. This will help the model recognize that the days of the week form a repeating cycle.

import numpy as np

# Solution: Encode Day of the Week as cyclical features
df['DayOfWeek_sin'] = np.sin(2 * np.pi * df['DayOfWeek'] / 7)
df['DayOfWeek_cos'] = np.cos(2 * np.pi * df['DayOfWeek'] / 7)

print("Dataset with cyclical encoding for Day of the Week:")
print(df[['DayOfWeek', 'DayOfWeek_sin', 'DayOfWeek_cos']])

These exercises cover the fundamentals of working with date/time, lagged, and rolling features in time series data, along with cyclical encoding techniques. By practicing these methods, you’ll gain a deeper understanding of how to create and interpret time-dependent features, which are essential for effective time series forecasting. 

9.3 Practical Exercises for Chapter 9

These exercises will help you practice creating and interpreting date/time features, lagged features, and rolling features in time series data. Each exercise builds on the techniques discussed in this chapter, allowing you to deepen your understanding of time series data manipulation and feature engineering.

Exercise 1: Extracting Date/Time Features

You are given a dataset with daily sales records. Your task is to:

  1. Convert the Date column to datetime format.
  2. Extract the YearMonthDay of the Week, and Quarter as separate features.
import pandas as pd

# Sample data with dates
data = {'Date': ['2022-01-15', '2022-02-10', '2022-03-20', '2022-04-15', '2022-05-25'],
        'Sales': [200, 220, 250, 210, 230]}
df = pd.DataFrame(data)

# Solution: Convert Date column to datetime and extract date/time features
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Quarter'] = df['Date'].dt.quarter

print("Dataset with extracted date/time features:")
print(df)

Exercise 2: Creating Lagged Features

Using the same dataset, create lagged features to represent sales from:

  1. The previous day (Sales_Lag1).
  2. Two days prior (Sales_Lag2).
# Solution: Create lagged features
df['Sales_Lag1'] = df['Sales'].shift(1)
df['Sales_Lag2'] = df['Sales'].shift(2)

print("Dataset with lagged features:")
print(df)

Exercise 3: Creating Rolling Features

Using the same dataset, calculate the following rolling statistics:

  1. 3-day rolling mean for the Sales column.
  2. 3-day rolling standard deviation for the Sales column.
# Solution: Create rolling mean and standard deviation
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()
df['RollingStd_3'] = df['Sales'].rolling(window=3).std()

print("Dataset with rolling features:")
print(df)

Exercise 4: Combining Date/Time, Lagged, and Rolling Features

In this exercise, you’ll create a combined dataset with both date/time features, lagged features, and rolling features. Using the previous dataset, complete the following tasks:

  1. Extract YearMonth, and Day of the Week.
  2. Create a Sales_Lag1 feature for the previous day’s sales.
  3. Calculate a 3-day rolling mean for the Sales column.
# Solution: Combine date/time, lagged, and rolling features
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Sales_Lag1'] = df['Sales'].shift(1)
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()

print("Combined dataset with date/time, lagged, and rolling features:")
print(df)

Exercise 5: Encoding Cyclical Features

For the Day of the Week feature in the previous dataset, apply sine and cosine encoding to capture its cyclical nature. This will help the model recognize that the days of the week form a repeating cycle.

import numpy as np

# Solution: Encode Day of the Week as cyclical features
df['DayOfWeek_sin'] = np.sin(2 * np.pi * df['DayOfWeek'] / 7)
df['DayOfWeek_cos'] = np.cos(2 * np.pi * df['DayOfWeek'] / 7)

print("Dataset with cyclical encoding for Day of the Week:")
print(df[['DayOfWeek', 'DayOfWeek_sin', 'DayOfWeek_cos']])

These exercises cover the fundamentals of working with date/time, lagged, and rolling features in time series data, along with cyclical encoding techniques. By practicing these methods, you’ll gain a deeper understanding of how to create and interpret time-dependent features, which are essential for effective time series forecasting. 

9.3 Practical Exercises for Chapter 9

These exercises will help you practice creating and interpreting date/time features, lagged features, and rolling features in time series data. Each exercise builds on the techniques discussed in this chapter, allowing you to deepen your understanding of time series data manipulation and feature engineering.

Exercise 1: Extracting Date/Time Features

You are given a dataset with daily sales records. Your task is to:

  1. Convert the Date column to datetime format.
  2. Extract the YearMonthDay of the Week, and Quarter as separate features.
import pandas as pd

# Sample data with dates
data = {'Date': ['2022-01-15', '2022-02-10', '2022-03-20', '2022-04-15', '2022-05-25'],
        'Sales': [200, 220, 250, 210, 230]}
df = pd.DataFrame(data)

# Solution: Convert Date column to datetime and extract date/time features
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Quarter'] = df['Date'].dt.quarter

print("Dataset with extracted date/time features:")
print(df)

Exercise 2: Creating Lagged Features

Using the same dataset, create lagged features to represent sales from:

  1. The previous day (Sales_Lag1).
  2. Two days prior (Sales_Lag2).
# Solution: Create lagged features
df['Sales_Lag1'] = df['Sales'].shift(1)
df['Sales_Lag2'] = df['Sales'].shift(2)

print("Dataset with lagged features:")
print(df)

Exercise 3: Creating Rolling Features

Using the same dataset, calculate the following rolling statistics:

  1. 3-day rolling mean for the Sales column.
  2. 3-day rolling standard deviation for the Sales column.
# Solution: Create rolling mean and standard deviation
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()
df['RollingStd_3'] = df['Sales'].rolling(window=3).std()

print("Dataset with rolling features:")
print(df)

Exercise 4: Combining Date/Time, Lagged, and Rolling Features

In this exercise, you’ll create a combined dataset with both date/time features, lagged features, and rolling features. Using the previous dataset, complete the following tasks:

  1. Extract YearMonth, and Day of the Week.
  2. Create a Sales_Lag1 feature for the previous day’s sales.
  3. Calculate a 3-day rolling mean for the Sales column.
# Solution: Combine date/time, lagged, and rolling features
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Sales_Lag1'] = df['Sales'].shift(1)
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()

print("Combined dataset with date/time, lagged, and rolling features:")
print(df)

Exercise 5: Encoding Cyclical Features

For the Day of the Week feature in the previous dataset, apply sine and cosine encoding to capture its cyclical nature. This will help the model recognize that the days of the week form a repeating cycle.

import numpy as np

# Solution: Encode Day of the Week as cyclical features
df['DayOfWeek_sin'] = np.sin(2 * np.pi * df['DayOfWeek'] / 7)
df['DayOfWeek_cos'] = np.cos(2 * np.pi * df['DayOfWeek'] / 7)

print("Dataset with cyclical encoding for Day of the Week:")
print(df[['DayOfWeek', 'DayOfWeek_sin', 'DayOfWeek_cos']])

These exercises cover the fundamentals of working with date/time, lagged, and rolling features in time series data, along with cyclical encoding techniques. By practicing these methods, you’ll gain a deeper understanding of how to create and interpret time-dependent features, which are essential for effective time series forecasting. 

9.3 Practical Exercises for Chapter 9

These exercises will help you practice creating and interpreting date/time features, lagged features, and rolling features in time series data. Each exercise builds on the techniques discussed in this chapter, allowing you to deepen your understanding of time series data manipulation and feature engineering.

Exercise 1: Extracting Date/Time Features

You are given a dataset with daily sales records. Your task is to:

  1. Convert the Date column to datetime format.
  2. Extract the YearMonthDay of the Week, and Quarter as separate features.
import pandas as pd

# Sample data with dates
data = {'Date': ['2022-01-15', '2022-02-10', '2022-03-20', '2022-04-15', '2022-05-25'],
        'Sales': [200, 220, 250, 210, 230]}
df = pd.DataFrame(data)

# Solution: Convert Date column to datetime and extract date/time features
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Quarter'] = df['Date'].dt.quarter

print("Dataset with extracted date/time features:")
print(df)

Exercise 2: Creating Lagged Features

Using the same dataset, create lagged features to represent sales from:

  1. The previous day (Sales_Lag1).
  2. Two days prior (Sales_Lag2).
# Solution: Create lagged features
df['Sales_Lag1'] = df['Sales'].shift(1)
df['Sales_Lag2'] = df['Sales'].shift(2)

print("Dataset with lagged features:")
print(df)

Exercise 3: Creating Rolling Features

Using the same dataset, calculate the following rolling statistics:

  1. 3-day rolling mean for the Sales column.
  2. 3-day rolling standard deviation for the Sales column.
# Solution: Create rolling mean and standard deviation
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()
df['RollingStd_3'] = df['Sales'].rolling(window=3).std()

print("Dataset with rolling features:")
print(df)

Exercise 4: Combining Date/Time, Lagged, and Rolling Features

In this exercise, you’ll create a combined dataset with both date/time features, lagged features, and rolling features. Using the previous dataset, complete the following tasks:

  1. Extract YearMonth, and Day of the Week.
  2. Create a Sales_Lag1 feature for the previous day’s sales.
  3. Calculate a 3-day rolling mean for the Sales column.
# Solution: Combine date/time, lagged, and rolling features
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['DayOfWeek'] = df['Date'].dt.dayofweek
df['Sales_Lag1'] = df['Sales'].shift(1)
df['RollingMean_3'] = df['Sales'].rolling(window=3).mean()

print("Combined dataset with date/time, lagged, and rolling features:")
print(df)

Exercise 5: Encoding Cyclical Features

For the Day of the Week feature in the previous dataset, apply sine and cosine encoding to capture its cyclical nature. This will help the model recognize that the days of the week form a repeating cycle.

import numpy as np

# Solution: Encode Day of the Week as cyclical features
df['DayOfWeek_sin'] = np.sin(2 * np.pi * df['DayOfWeek'] / 7)
df['DayOfWeek_cos'] = np.cos(2 * np.pi * df['DayOfWeek'] / 7)

print("Dataset with cyclical encoding for Day of the Week:")
print(df[['DayOfWeek', 'DayOfWeek_sin', 'DayOfWeek_cos']])

These exercises cover the fundamentals of working with date/time, lagged, and rolling features in time series data, along with cyclical encoding techniques. By practicing these methods, you’ll gain a deeper understanding of how to create and interpret time-dependent features, which are essential for effective time series forecasting.