11  Pandas Time And Text Practice

This notebook focuses on datetime features, simple time-based summaries, and text cleanup.

import pandas as pd

events = pd.DataFrame(
    {
        "timestamp": [
            "2026-01-01 09:15:00",
            "2026-01-01 10:05:00",
            "2026-01-02 11:45:00",
            "2026-01-02 12:10:00",
        ],
        "event_type": ["click", "purchase", "click", "support"],
        "message": [
            "Landing page click",
            "Completed Purchase",
            "Clicked pricing section",
            "Need HELP with invoice",
        ],
    }
)

events["timestamp"] = pd.to_datetime(events["timestamp"])
events

11.1 Exercise 1

Create date and hour columns from timestamp.

# TODO: Extract both date and hour from the timestamp column.
events["date"] = events["timestamp"].dt.____
events["hour"] = events["timestamp"].dt.____
events

11.2 Exercise 2

Count how many events happened on each date.

# TODO: Group by date and count the number of rows.
events_per_day = events.groupby("____").size()
events_per_day

11.3 Exercise 3

Create a lowercase version of message called message_clean.

# TODO: Create a lowercase text column.
events["message_clean"] = events["message"].str.____()
events

11.4 Exercise 4

Create a boolean column called mentions_help that is True when the cleaned message contains the word help.

# TODO: Detect whether each cleaned message contains the word help.
events["mentions_help"] = events["message_clean"].str.contains("____")
events

11.5 Exercise 5

Filter the rows where event_type is click.

# TODO: Filter the table down to click events only.
click_events = events[events["event_type"] == "____"]
click_events

11.6 Reflection

How would these datetime and text transformations support a downstream ML or analytics workflow?