Data Science · Chapter 35 of 43

Time Series Basics

TIME SERIES data is indexed by time (daily sales, hourly temperature). Order matters — splits must be chronological.

Common tasks: forecasting, anomaly detection, trend/seasonality decomposition.

Example 1 (python)
import pandas as pd
df = pd.read_csv('sales.csv', parse_dates=['date'], index_col='date')
print(df.resample('M').sum().head())

Monthly totals.

Example 2 (python)
df['rolling_7'] = df['sales'].rolling(7).mean()

7-day moving average.

Key points

  • Order matters — split chronologically.
  • Common patterns: trend + seasonality.
  • Resampling changes frequency.
  • Rolling stats smooth noise.
💡 Note: Never randomly shuffle time-series data — you'll leak the future into the past and get unrealistically great scores.

📝 Quick Quiz

1. Time-series splits should be:

2. df.resample('M') gives:

3. A rolling average is used to: