Rows picked by a date condition and the points on a zoomed chart
Predict the output · Medium · §10.3 Date axes and range exploration
The code makes a table of monthly bookings at two tennis courts for six months from April, picks rows with a date condition, and then narrows the x-axis of a line chart to July to September. Predict the four lines of output without running the code.
Code
import pandas as pd
import plotly.express as px
dates = pd.date_range("2026-04-01", periods=6, freq="MS")
courts = pd.DataFrame({
"date": list(dates) * 2,
"court": ["Clay"] * 6 + ["Grass"] * 6,
"bookings": [40, 52, 61, 75, 70, 48, 35, 44, 58, 66, 72, 50],
})
print(courts["date"].dt.month.tolist()[:6])
summer = courts[courts["date"].dt.month >= 7]
print(len(courts), len(summer))
print(summer.groupby("court")["bookings"].sum().tolist())
fig = px.line(courts, x="date", y="bookings", color="court")
fig.update_xaxes(rangeslider_visible=True, range=["2026-07-01", "2026-09-01"])
print(len(fig.data[0].x), len(courts))Solve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.