Editor

Build the Time Series DataFrame

Advanced · Write the code · from Time Series: Detect Trends and Anomalies in Business Data

From the lesson: Step 1: Create a Time Series Dataset

Time series data has a special structure: each row represents a point in time. The index (or a date column) tells pandas the temporal order. We'll generate 90 days of daily sales data with a built-in upward trend, weekly seasonality, and some random noise.

Read this part of the lesson

Create a pandas DataFrame with 90 days of synthetic daily sales data:

1. Use pd.date_range('2024-01-01', periods=90, freq='D') for dates.

2. Generate sales with a base of 1000, a daily increase of 5 (trend), weekend boost of 200, and random noise using np.random.seed(42) and np.random.normal(0, 80, 90).

3. Set the date as the index.

4. Print the first 5 rows, the shape, and the mean sales formatted as "Mean daily sales: $X.XX".

import pandas as pd
import numpy as np
# Create 90 days of sales data with trend + seasonality + noise

More Time Series exercises

All Python exercises · 50 practice problems · Practical Projects quiz