Pandas Lesson 25 – Time Series | Dataplexa

Time Series Analysis in Pandas

Many real-world datasets are based on time: sales per day, stock prices per minute, website traffic per hour, or temperature per month.

Pandas provides powerful tools to work with date and time based data, commonly known as time series.

In this lesson, you will learn how to parse dates, set time indexes, and analyze data over time.


What Is Time Series Data?

Time series data is any data where each record is associated with a timestamp.

Examples:

  • Daily sales reports
  • Monthly revenue
  • Hourly sensor readings
  • Stock market prices

Converting a Column to DateTime

Most datasets store dates as strings. Pandas needs them in a special datetime format to perform time-based operations.

Example: Convert an order date column.

import pandas as pd

sales = pd.read_csv("dataplexa_pandas_sales.csv")

sales["order_date"] = pd.to_datetime(sales["order_date"])

Once converted, Pandas understands year, month, day, and even time automatically.


Setting Date as Index

For time series analysis, it is common to set the date column as the DataFrame index.

sales.set_index("order_date", inplace=True)

This allows fast filtering and resampling by date.


Filtering Data by Date

Once the index is a datetime, you can filter data using simple syntax.

Example: Select all sales from 2024.

sales.loc["2024"]

Example: Select sales from March 2024.

sales.loc["2024-03"]

Resampling Time Series Data

Resampling means changing the frequency of time data.

Common resampling rules:

  • D – Daily
  • M – Monthly
  • Y – Yearly

Example: Monthly total sales.

monthly_sales = sales["sales_amount"].resample("M").sum()

Resampling with Multiple Aggregations

You can apply multiple calculations while resampling.

monthly_summary = sales.resample("M").agg(
    total_sales=("sales_amount", "sum"),
    average_sales=("sales_amount", "mean"),
    orders=("sales_amount", "count")
)

Extracting Date Components

Sometimes you only need parts of a date like year or month.

sales["year"] = sales.index.year
sales["month"] = sales.index.month
sales["day"] = sales.index.day

This is useful for grouping and trend analysis.


Rolling Window Calculations

Rolling windows calculate statistics over a moving time window.

Example: 7-day rolling average of sales.

sales["rolling_avg"] = sales["sales_amount"].rolling(7).mean()

Rolling averages help smooth noisy data and reveal trends.


Common Use Cases

  • Sales trend analysis
  • Forecast preparation
  • Seasonality detection
  • Business reporting

Practice Exercise

Using the dataset:

  • Convert the date column to datetime
  • Calculate monthly total sales
  • Find the month with highest sales
  • Create a rolling average

What’s Next?

In the next lesson, you will learn about MultiIndex and how Pandas handles hierarchical indexing.