A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Fix reactive Plotly heatmap selection with datetime axes (#7805)
Florian Verdonck1a6960138394
diff --git a/marimo/_plugins/ui/_impl/plotly.py b/marimo/_plugins/ui/_impl/plotly.pyindex aadb22c76e..4ec5835b6b 100644--- a/marimo/_plugins/ui/_impl/plotly.py+++ b/marimo/_plugins/ui/_impl/plotly.py@@ -37,6 +37,95 @@ if TYPE_CHECKING:PlotlySelection = dict[str, JSONType]+def _is_orderable_value(value: Any) -> bool:+ """Check if a value is orderable (numeric or datetime-like).++ Used by fallback functions to determine if direct comparison is possible.+ """+ import datetime++ if isinstance(value, (int, float, datetime.datetime, datetime.date)):+ return True++ # Check for ISO format datetime strings (sent from frontend via JSON)+ if isinstance(value, str):+ try:+ datetime.datetime.fromisoformat(value.replace("Z", "+00:00"))+ return True+ except (ValueError, AttributeError):+ pass++ return False+++def _parse_datetime_bound(value: Any) -> Any:+ """Parse a bound value that might be a datetime string from the frontend.++ Returns the original value if it's already a datetime or not a valid+ datetime string.+ """+ import datetime++ if isinstance(value, (datetime.datetime, datetime.date)):+ return value++ if isinstance(value, str):+ try:+ return datetime.datetime.fromisoformat(+ value.replace("Z", "+00:00")+ )+ except (ValueError, AttributeError):+ pass++ return value+++def _is_orderable_axis(arr: Any, bound_value: Any) -> bool:+ """Check if an axis contains orderable (numeric/datetime) values.++ This checks both numpy dtype and the actual element types to handle:+ - Native numpy numeric/datetime64 arrays+ - Object arrays containing Python datetime.datetime objects+ - Datetime strings from frontend JSON (ISO format)++ Args:+ arr: A numpy array of axis values+ bound_value: A sample bound value (e.g., x_min) to check compatibility++ Returns:+ True if the axis values can be compared with the bound values+ """+ import datetime++ import numpy as np++ # Check numpy dtype first (handles native numpy types)+ if np.issubdtype(arr.dtype, np.number):+ return True+ if np.issubdtype(arr.dtype, np.datetime64):+ return True++ # For object arrays, check element types and bound value compatibility+ if arr.dtype == np.object_ and len(arr) > 0:+ first_elem = arr[0]+ # Check if elements are datetime-like+ if isinstance(first_elem, (datetime.datetime, datetime.date)):+ # Bound can be datetime object or ISO string from frontend+ if isinstance(bound_value, (datetime.datetime, datetime.date)):+ return True+ # Check for ISO format datetime strings (sent from frontend via JSON)+ if isinstance(bound_value, str):+ try:+ datetime.datetime.fromisoformat(+ bound_value.replace("Z", "+00:00")+ )+ return True+ except (ValueError, AttributeError):+ pass++ return False++@mddocclass plotly(UIElement[PlotlySelection, list[dict[str, Any]]]):"""Make reactive plots with Plotly.@@ -376,21 +465,35 @@ def _extract_heatmap_cells_numpy(y_arr = np.asarray(y_data)z_arr = np.asarray(z_data)- # Determine if axes are numeric- x_is_numeric = np.issubdtype(x_arr.dtype, np.number)- y_is_numeric = np.issubdtype(y_arr.dtype, np.number)+ # Determine if axes are orderable (numeric or datetime-like)+ x_is_orderable = _is_orderable_axis(x_arr, x_min)+ y_is_orderable = _is_orderable_axis(y_arr, y_min)++ # Parse datetime bounds (frontend sends ISO strings via JSON)+ x_min_parsed = (+ _parse_datetime_bound(x_min) if x_is_orderable else x_min+ )+ x_max_parsed = (+ _parse_datetime_bound(x_max) if x_is_orderable else x_max+ )+ y_min_parsed = (+ _parse_datetime_bound(y_min) if y_is_orderable else y_min+ )+ y_max_parsed = (+ _parse_datetime_bound(y_max) if y_is_orderable else y_max+ )# Compute masks for valid indices- if x_is_numeric:- x_mask = (x_arr >= x_min) & (x_arr <= x_max)+ if x_is_orderable:+ x_mask = (x_arr >= x_min_parsed) & (x_arr <= x_max_parsed)else:# Categorical: cell spans (index - 0.5) to (index + 0.5)# Strict overlap: x_max > cell_x_min and x_min < cell_x_maxx_indices = np.arange(len(x_arr))x_mask = (x_max > x_indices - 0.5) & (x_min < x_indices + 0.5)- if y_is_numeric:- y_mask = (y_arr >= y_min) & (y_arr <= y_max)+ if y_is_orderable:+ y_mask = (y_arr >= y_min_parsed) & (y_arr <= y_max_parsed)else:# Categorical: cell spans (index - 0.5) to (index + 0.5)# Strict overlap: y_max > cell_y_min and y_min < cell_y_max@@ -543,12 +646,20 @@ def _extract_scatter_points_numpy(x_arr = np.asarray(x_data)y_arr = np.asarray(y_data)- # Check if x is numeric- x_is_numeric = np.issubdtype(x_arr.dtype, np.number)+ # Check if x is orderable (numeric or datetime-like)+ x_is_orderable = _is_orderable_axis(x_arr, x_min)++ # Parse datetime bounds (frontend sends ISO strings via JSON)+ x_min_parsed = (+ _parse_datetime_bound(x_min) if x_is_orderable else x_min+ )+ x_max_parsed = (+ _parse_datetime_bound(x_max) if x_is_orderable else x_max+ )# Filter by x-range (matching Altair behavior)- if x_is_numeric:- x_mask = (x_arr >= x_min) & (x_arr <= x_max)+ if x_is_orderable:+ x_mask = (x_arr >= x_min_parsed) & (x_arr <= x_max_parsed)else:# Categorical: use index-based filteringx_indices = np.arange(len(x_arr))@@ -623,8 +734,11 @@ def _extract_scatter_points_fallback(# Check if x is within rangex_in_range = False- if isinstance(x_val, (int, float)):- x_in_range = x_min <= x_val <= x_max+ if _is_orderable_value(x_val) and _is_orderable_value(x_min):+ # Parse datetime bounds (frontend sends ISO strings)+ x_min_p = _parse_datetime_bound(x_min)+ x_max_p = _parse_datetime_bound(x_max)+ x_in_range = x_min_p <= x_val <= x_max_pelse:# Categorical - use index-based filteringcell_x_min = point_idx - 0.5@@ -678,8 +792,11 @@ def _extract_heatmap_cells_fallback(x_in_range = Falsey_in_range = False- if isinstance(x_val, (int, float)):- x_in_range = x_min <= x_val <= x_max+ if _is_orderable_value(x_val) and _is_orderable_value(x_min):+ # Parse datetime bounds (frontend sends ISO strings)+ x_min_p = _parse_datetime_bound(x_min)+ x_max_p = _parse_datetime_bound(x_max)+ x_in_range = x_min_p <= x_val <= x_max_pelse:# Categorical - each cell spans from (index - 0.5) to (index + 0.5)# Include cell if selection range strictly overlaps with cell bounds@@ -689,8 +806,11 @@ def _extract_heatmap_cells_fallback(x_max <= cell_x_min or x_min >= cell_x_max)- if isinstance(y_val, (int, float)):- y_in_range = y_min <= y_val <= y_max+ if _is_orderable_value(y_val) and _is_orderable_value(y_min):+ # Parse datetime bounds (frontend sends ISO strings)+ y_min_p = _parse_datetime_bound(y_min)+ y_max_p = _parse_datetime_bound(y_max)+ y_in_range = y_min_p <= y_val <= y_max_pelse:# Categorical - each cell spans from (index - 0.5) to (index + 0.5)# Include cell if selection range strictly overlaps with cell bounds@@ -811,15 +931,25 @@ def _extract_bars_numpy(value_data = x_arrpos_min, pos_max = y_min, y_max- # Determine if position axis is numeric or categorical- pos_is_numeric = np.issubdtype(position_data.dtype, np.number)+ # Determine if position axis is orderable (numeric or datetime-like)+ pos_is_orderable = _is_orderable_axis(position_data, pos_min)++ # Parse datetime bounds (frontend sends ISO strings via JSON)+ pos_min_parsed = (+ _parse_datetime_bound(pos_min) if pos_is_orderable else pos_min+ )+ pos_max_parsed = (+ _parse_datetime_bound(pos_max) if pos_is_orderable else pos_max+ )# Compute mask for valid indices- if pos_is_numeric:- # Numeric axis: direct range comparison+ if pos_is_orderable:+ # Orderable axis: direct range comparison# TODO: Consider bar width in future implementations# Currently treating bars as points at their position value- pos_mask = (position_data >= pos_min) & (position_data <= pos_max)+ pos_mask = (position_data >= pos_min_parsed) & (+ position_data <= pos_max_parsed+ )else:# Categorical axis: each bar spans (index - 0.5) to (index + 0.5)# Selection overlaps if: pos_max > bar_min AND pos_min < bar_max@@ -898,11 +1028,14 @@ def _extract_bars_fallback(# Check if bar position is within selection rangepos_in_range = False- if isinstance(pos_val, (int, float)):- # Numeric axis: direct comparison+ if _is_orderable_value(pos_val) and _is_orderable_value(pos_min):+ # Orderable axis (numeric or datetime): direct comparison+ # Parse datetime bounds (frontend sends ISO strings)+ pos_min_p = _parse_datetime_bound(pos_min)+ pos_max_p = _parse_datetime_bound(pos_max)# TODO: Consider bar width in future implementations# Currently treating bars as points at their position value- pos_in_range = pos_min <= pos_val <= pos_max+ pos_in_range = pos_min_p <= pos_val <= pos_max_pelse:# Categorical axis: each bar spans (index - 0.5) to (index + 0.5)# Include bar if selection range strictly overlaps with bar boundsdiff --git a/tests/_plugins/ui/_impl/test_plotly.py b/tests/_plugins/ui/_impl/test_plotly.pyindex cf326f52b3..79e4d67a2c 100644--- a/tests/_plugins/ui/_impl/test_plotly.py+++ b/tests/_plugins/ui/_impl/test_plotly.py@@ -1,6 +1,7 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations+from datetime import datetime, timedeltafrom typing import Anyimport pytest@@ -770,6 +771,212 @@ def test_heatmap_numpy_and_fallback_produce_same_results() -> None:assert np_cell["curveNumber"] == fb_cell["curveNumber"]+# ============================================================================+# Datetime Axis Tests+# ============================================================================+++def test_heatmap_selection_datetime_x_axis() -> None:+ """Test heatmap with datetime x-axis and categorical y-axis.++ This is the exact scenario reported in the bug: datetime x-axis,+ string y-axis, which was causing a numpy UFuncNoLoopError.+ """++ # Create datetime x-axis+ base_date = datetime(2024, 1, 1)+ dates = [base_date + timedelta(days=i) for i in range(5)]++ # String categories for y-axis+ categories = ["Category A", "Category B", "Category C"]++ # Generate z values (3 rows x 5 columns)+ z_values = [[i * 5 + j for j in range(5)] for i in range(3)]++ fig = go.Figure(+ data=go.Heatmap(+ z=z_values,+ x=dates,+ y=categories,+ )+ )+ plot = plotly(fig)++ # Simulate a range selection covering dates[1] to dates[3]+ # and categories at indices 0 and 1+ # IMPORTANT: Frontend sends ISO strings via JSON, not datetime objects+ selection = {+ "range": {+ "x": [dates[1].isoformat(), dates[3].isoformat()],+ "y": [-0.5, 1.5],+ },+ "points": [],+ "indices": [],+ }++ result = plot._convert_value(selection)++ # Should extract cells for dates[1], dates[2], dates[3]+ # and categories at indices 0, 1+ # That's 3 dates x 2 categories = 6 cells+ assert len(result) == 6++ # Verify we got the expected cells+ for cell in result:+ assert cell["x"] in dates[1:4] # dates[1], dates[2], dates[3]+ assert cell["y"] in ["Category A", "Category B"]+ assert "z" in cell+++def test_heatmap_numpy_and_fallback_datetime_x_axis() -> None:+ """Test that numpy and fallback produce same results for datetime x-axis."""+ base_date = datetime(2024, 1, 1)+ dates = [base_date + timedelta(days=i) for i in range(5)]+ categories = ["A", "B", "C"]+ z_values = [[i * 5 + j for j in range(5)] for i in range(3)]++ fig = go.Figure(data=go.Heatmap(z=z_values, x=dates, y=categories))++ # Use ISO strings for x range (as frontend sends), indices for y range+ x_min, x_max = dates[1].isoformat(), dates[3].isoformat()+ y_min, y_max = -0.5, 1.5++ numpy_result = _extract_heatmap_cells_numpy(+ fig, x_min, x_max, y_min, y_max+ )+ fallback_result = _extract_heatmap_cells_fallback(+ fig, x_min, x_max, y_min, y_max+ )++ assert len(numpy_result) == len(fallback_result)++ # Sort for comparison+ def sort_key(cell: dict[str, Any]) -> tuple[Any, ...]:+ return (str(cell["x"]), cell["y"], cell["z"])++ numpy_sorted = sorted(numpy_result, key=sort_key)+ fallback_sorted = sorted(fallback_result, key=sort_key)++ for np_cell, fb_cell in zip(numpy_sorted, fallback_sorted):+ assert np_cell["x"] == fb_cell["x"]+ assert np_cell["y"] == fb_cell["y"]+ assert np_cell["z"] == fb_cell["z"]+++def test_scatter_selection_datetime_x_axis() -> None:+ """Test scatter/line chart with datetime x-axis."""+ base_date = datetime(2024, 1, 1)+ dates = [base_date + timedelta(days=i) for i in range(5)]+ values = [10, 20, 15, 25, 30]++ fig = go.Figure(data=go.Scatter(x=dates, y=values, mode="lines"))+ plot = plotly(fig)++ # Select dates[1] to dates[3] - frontend sends ISO strings+ selection = {+ "range": {+ "x": [dates[1].isoformat(), dates[3].isoformat()],+ "y": [0, 50],+ },+ "points": [],+ "indices": [],+ }++ result = plot._convert_value(selection)++ # Should return 3 points: at dates[1], dates[2], dates[3]+ assert len(result) == 3+ assert result[0]["x"] == dates[1]+ assert result[0]["y"] == 20+ assert result[1]["x"] == dates[2]+ assert result[1]["y"] == 15+ assert result[2]["x"] == dates[3]+ assert result[2]["y"] == 25+++def test_scatter_numpy_and_fallback_datetime_x_axis() -> None:+ """Test that numpy and fallback produce same results for datetime scatter."""+ base_date = datetime(2024, 1, 1)+ dates = [base_date + timedelta(days=i) for i in range(5)]+ values = [10, 20, 15, 25, 30]++ fig = go.Figure(data=go.Scatter(x=dates, y=values, mode="lines"))++ # Frontend sends ISO strings+ x_min, x_max = dates[1].isoformat(), dates[3].isoformat()++ numpy_result = _extract_scatter_points_numpy(fig, x_min, x_max)+ fallback_result = _extract_scatter_points_fallback(fig, x_min, x_max)++ assert len(numpy_result) == len(fallback_result)++ def sort_key(p: dict[str, Any]) -> tuple[Any, ...]:+ return (str(p["x"]), p["y"])++ numpy_sorted = sorted(numpy_result, key=sort_key)+ fallback_sorted = sorted(fallback_result, key=sort_key)++ for np_p, fb_p in zip(numpy_sorted, fallback_sorted):+ assert np_p["x"] == fb_p["x"]+ assert np_p["y"] == fb_p["y"]+++def test_bar_selection_datetime_x_axis() -> None:+ """Test bar chart with datetime x-axis."""+ base_date = datetime(2024, 1, 1)+ dates = [base_date + timedelta(days=i) for i in range(5)]+ values = [10, 20, 15, 25, 30]++ fig = go.Figure(data=go.Bar(x=dates, y=values))+ plot = plotly(fig)++ # Select dates[1] to dates[3] - frontend sends ISO strings+ selection = {+ "range": {+ "x": [dates[1].isoformat(), dates[3].isoformat()],+ "y": [0, 50],+ },+ "points": [],+ "indices": [],+ }++ result = plot._convert_value(selection)++ # Should return 3 bars: at dates[1], dates[2], dates[3]+ assert len(result) == 3+ assert any(bar["x"] == dates[1] and bar["y"] == 20 for bar in result)+ assert any(bar["x"] == dates[2] and bar["y"] == 15 for bar in result)+ assert any(bar["x"] == dates[3] and bar["y"] == 25 for bar in result)+++def test_bar_numpy_and_fallback_datetime_x_axis() -> None:+ """Test that numpy and fallback produce same results for datetime bars."""+ base_date = datetime(2024, 1, 1)+ dates = [base_date + timedelta(days=i) for i in range(5)]+ values = [10, 20, 15, 25, 30]++ fig = go.Figure(data=go.Bar(x=dates, y=values))++ # Frontend sends ISO strings+ x_min, x_max = dates[1].isoformat(), dates[3].isoformat()+ y_min, y_max = 0, 50++ numpy_result = _extract_bars_numpy(fig, x_min, x_max, y_min, y_max)+ fallback_result = _extract_bars_fallback(fig, x_min, x_max, y_min, y_max)++ assert len(numpy_result) == len(fallback_result)++ def sort_key(bar: dict[str, Any]) -> tuple[Any, ...]:+ return (str(bar["x"]), bar["y"])++ numpy_sorted = sorted(numpy_result, key=sort_key)+ fallback_sorted = sorted(fallback_result, key=sort_key)++ for np_bar, fb_bar in zip(numpy_sorted, fallback_sorted):+ assert np_bar["x"] == fb_bar["x"]+ assert np_bar["y"] == fb_bar["y"]++# ============================================================================# Scatter / Line Chart Tests# ============================================================================