Goal
Given Uber ride/trip records:
- Find peak hours and weekdays
- Visualize trip volume over time
- Identify hotspots (if location data exists)
Analysis pipeline
flowchart LR A["Raw trips
(timestamps)"] --> B["Extract
(hour, weekday)"] B --> C["Count
(trips per hour/day)"] C --> D["Visualize
(demand curve)"] D --> E["Conclude
(peak hours/days)"]
Step 1: Load and parse timestamps
import pandas as pd
df = pd.read_csv("data/uber.csv")
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
print(df.head())Step 2: Extract time features
df["hour"] = df["timestamp"].dt.hour
df["weekday"] = df["timestamp"].dt.day_name()Step 3: Plot trips by hour
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
sns.countplot(data=df, x="hour")
plt.title("Trips by hour")
plt.tight_layout()
plt.show()Step 4: Trend over dates
import matplotlib.pyplot as plt
daily = df.dropna(subset=["timestamp"]).groupby(df["timestamp"].dt.date).size()
plt.figure(figsize=(10, 4))
plt.plot(daily.index, daily.values)
plt.title("Trips over time")
plt.xticks(rotation=20)
plt.tight_layout()
plt.show()Visualize it
Deliverable
- Peak hours
- Peak weekdays
- Trend changes (seasonality/events)
Check your understanding
Which pandas feature extracts the hour from the timestamp column?
The dt accessor works on datetime columns, after converting with pd.to_datetime.
Show answer
A — df["timestamp"].dt.hour — The dt accessor works on datetime columns, after converting with pd.to_datetime.
What does sns.countplot(data=df, x="hour") show?
It counts rows per hour, a frequency table of timestamps, which reveals peak hours.
Show answer
A — The number of trips in each hour of the day — It counts rows per hour, a frequency table of timestamps, which reveals peak hours.
How are trips per calendar day computed?
It uses groupby(df["timestamp"].dt.date).size() on rows that have valid timestamps.
Show answer
A — Group by timestamp.dt.date after dropping missing timestamps and count rows — It uses groupby(df["timestamp"].dt.date).size() on rows that have valid timestamps.
What does dt.day_name() give you for the weekday analysis?
It converts each timestamp to a weekday name so you can compare peak weekdays.
Show answer
A — Text names such as Monday for each timestamp — It converts each timestamp to a weekday name so you can compare peak weekdays.
🧪 Try It Yourself
Exercise 1 – Extract the hour from a timestamp
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Count trips per hour
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Weekday name from a date
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.