Skip to content

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.

fix(ui): handle empty series initializers (#11319)

Kevin Joseph879cf8e46d81
diff --git a/marimo/_data/series.py b/marimo/_data/series.py
index 8ad82696fc..9b0ab1396a 100644
--- a/marimo/_data/series.py
+++ b/marimo/_data/series.py
@@ -22,8 +22,8 @@ class NumberSeriesInfo:
Represents a summary of a numeric series.
"""
- min: float
- max: float
+ min: float | None
+ max: float | None
label: str
@@ -43,8 +43,8 @@ class DateSeriesInfo:
Represents a summary of a date series.
"""
- min: str
- max: str
+ min: str | None
+ max: str | None
label: str
@@ -54,6 +54,15 @@ def _get_name(series: nw.Series[Any]) -> str:
return str(series.name)
+def _is_missing(value: Any) -> bool:
+ if value is None:
+ return True
+ try:
+ return bool(value != value) # noqa: PLR0124
+ except (TypeError, ValueError):
+ return False
+
+
@nw.narwhalify(eager_only=True, series_only=True)
def get_number_series_info(series: nw.Series[Any]) -> NumberSeriesInfo:
"""
@@ -62,17 +71,32 @@ def get_number_series_info(series: nw.Series[Any]) -> NumberSeriesInfo:
assert_narwhals_series(series)
series = series.drop_nulls()
+ if series.dtype.is_float():
+ # Arrow can return inf/-inf bounds when every value is NaN.
+ series = series.filter(~series.is_nan())
+ raw_min = series.min()
+ raw_max = series.max()
def validate_number(value: Any) -> float:
value = unwrap_py_scalar(value)
+ if _is_missing(value):
+ raise ValueError("Series has no non-null numeric values")
value = float(value)
if not isinstance(value, (int, float)):
raise ValueError("Expected a number. Got: " + str(type(value)))
return value
return NumberSeriesInfo(
- min=validate_number(series.min()),
- max=validate_number(series.max()),
+ min=(
+ None
+ if series.len() == 0 or _is_missing(raw_min)
+ else validate_number(raw_min)
+ ),
+ max=(
+ None
+ if series.len() == 0 or _is_missing(raw_max)
+ else validate_number(raw_max)
+ ),
label=_get_name(series),
)
@@ -100,9 +124,13 @@ def get_date_series_info(series: nw.Series[Any]) -> DateSeriesInfo:
assert_narwhals_series(series)
series = series.drop_nulls()
+ raw_min = series.min()
+ raw_max = series.max()
def validate_date(value: Any) -> str:
value = unwrap_py_scalar(value)
+ if _is_missing(value):
+ raise ValueError("Series has no non-null date values")
if isinstance(value, datetime.date):
return value.strftime("%Y-%m-%d")
if hasattr(value, "strftime"):
@@ -110,8 +138,16 @@ def get_date_series_info(series: nw.Series[Any]) -> DateSeriesInfo:
raise ValueError("Expected a date. Got: " + str(type(value)))
return DateSeriesInfo(
- min=validate_date(series.min()),
- max=validate_date(series.max()),
+ min=(
+ None
+ if series.len() == 0 or _is_missing(raw_min)
+ else validate_date(raw_min)
+ ),
+ max=(
+ None
+ if series.len() == 0 or _is_missing(raw_max)
+ else validate_date(raw_max)
+ ),
label=_get_name(series),
)
@@ -124,9 +160,13 @@ def get_datetime_series_info(series: nw.Series[Any]) -> DateSeriesInfo:
assert_narwhals_series(series)
series = series.drop_nulls()
+ raw_min = series.min()
+ raw_max = series.max()
def validate_datetime(value: Any) -> str:
value = unwrap_py_scalar(value)
+ if _is_missing(value):
+ raise ValueError("Series has no non-null datetime values")
if isinstance(value, datetime.datetime):
return value.strftime("%Y-%m-%dT%H:%M:%S")
if isinstance(value, datetime.date):
@@ -138,7 +178,15 @@ def get_datetime_series_info(series: nw.Series[Any]) -> DateSeriesInfo:
raise ValueError("Expected a datetime. Got: " + str(type(value)))
return DateSeriesInfo(
- min=validate_datetime(series.min()),
- max=validate_datetime(series.max()),
+ min=(
+ None
+ if series.len() == 0 or _is_missing(raw_min)
+ else validate_datetime(raw_min)
+ ),
+ max=(
+ None
+ if series.len() == 0 or _is_missing(raw_max)
+ else validate_datetime(raw_max)
+ ),
label=_get_name(series),
)
diff --git a/marimo/_plugins/ui/_impl/dates.py b/marimo/_plugins/ui/_impl/dates.py
index d43d0f941e..5a03ca75a7 100644
--- a/marimo/_plugins/ui/_impl/dates.py
+++ b/marimo/_plugins/ui/_impl/dates.py
@@ -25,6 +25,7 @@ if TYPE_CHECKING:
LOGGER = _loggers.marimo_logger()
Numeric = int | float
+NO_VALUE: Final = object()
@mddoc
@@ -148,9 +149,20 @@ class date(UIElement[str, dt.date]):
Returns:
date: A date picker initialized with the series' min and max dates as bounds.
"""
+ start = kwargs.pop("start", NO_VALUE)
+ stop = kwargs.pop("stop", NO_VALUE)
info = get_date_series_info(series)
- start = kwargs.pop("start", info.min)
- stop = kwargs.pop("stop", info.max)
+ if start is NO_VALUE:
+ start = info.min
+ if stop is NO_VALUE:
+ stop = info.max
+ if (start is NO_VALUE or start is None) and (
+ stop is NO_VALUE or stop is None
+ ):
+ raise ValueError(
+ "Cannot infer both start and stop from an empty or all-null series; "
+ "provide explicit bounds."
+ )
label = kwargs.pop("label", info.label)
return date(start=start, stop=stop, label=label, **kwargs)
@@ -300,9 +312,20 @@ class datetime(UIElement[str | None, dt.datetime | None]):
Returns:
datetime: A datetime picker initialized with the series' min and max datetimes as bounds.
"""
+ start = kwargs.pop("start", NO_VALUE)
+ stop = kwargs.pop("stop", NO_VALUE)
info = get_datetime_series_info(series)
- start = kwargs.pop("start", info.min)
- stop = kwargs.pop("stop", info.max)
+ if start is NO_VALUE:
+ start = info.min
+ if stop is NO_VALUE:
+ stop = info.max
+ if (start is NO_VALUE or start is None) and (
+ stop is NO_VALUE or stop is None
+ ):
+ raise ValueError(
+ "Cannot infer both start and stop from an empty or all-null series; "
+ "provide explicit bounds."
+ )
label = kwargs.pop("label", info.label)
return datetime(start=start, stop=stop, label=label, **kwargs)
@@ -461,9 +484,20 @@ class date_range(UIElement[tuple[str, str], tuple[dt.date, dt.date]]):
Returns:
date_range: A date range picker initialized with the series' min and max dates as bounds.
"""
+ start = kwargs.pop("start", NO_VALUE)
+ stop = kwargs.pop("stop", NO_VALUE)
info = get_date_series_info(series)
- start = kwargs.pop("start", info.min)
- stop = kwargs.pop("stop", info.max)
+ if start is NO_VALUE:
+ start = info.min
+ if stop is NO_VALUE:
+ stop = info.max
+ if (start is NO_VALUE or start is None) and (
+ stop is NO_VALUE or stop is None
+ ):
+ raise ValueError(
+ "Cannot infer both start and stop from an empty or all-null series; "
+ "provide explicit bounds."
+ )
label = kwargs.pop("label", info.label)
return date_range(start=start, stop=stop, label=label, **kwargs)
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 80d2ca0d04..276cf99f1a 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -35,6 +35,7 @@ from marimo._runtime.functions import Function
LOGGER = _loggers.marimo_logger()
Numeric = int | float
+NO_VALUE: Final = object()
@mddoc
@@ -131,9 +132,20 @@ class number(UIElement[Numeric | None, Numeric | None]):
@staticmethod
def from_series(series: DataFrameSeries, **kwargs: Any) -> number:
"""Create a number picker from a dataframe series."""
+ start = kwargs.pop("start", NO_VALUE)
+ stop = kwargs.pop("stop", NO_VALUE)
info = get_number_series_info(series)
- start = kwargs.pop("start", info.min)
- stop = kwargs.pop("stop", info.max)
+ if start is NO_VALUE:
+ start = info.min
+ if stop is NO_VALUE:
+ stop = info.max
+ if (start is NO_VALUE or start is None) and (
+ stop is NO_VALUE or stop is None
+ ):
+ raise ValueError(
+ "Cannot infer both start and stop from an empty or all-null series; "
+ "provide explicit bounds."
+ )
label = kwargs.pop("label", info.label)
return number(start=start, stop=stop, label=label, **kwargs)
@@ -350,9 +362,20 @@ class slider(UIElement[Numeric, Numeric]):
@staticmethod
def from_series(series: DataFrameSeries, **kwargs: Any) -> slider:
"""Create a slider from a dataframe series."""
+ start = kwargs.pop("start", NO_VALUE)
+ stop = kwargs.pop("stop", NO_VALUE)
info = get_number_series_info(series)
- start = kwargs.pop("start", info.min)
- stop = kwargs.pop("stop", info.max)
+ if start is NO_VALUE:
+ start = info.min
+ if stop is NO_VALUE:
+ stop = info.max
+ if (start is NO_VALUE or start is None) and (
+ stop is NO_VALUE or stop is None
+ ):
+ raise ValueError(
+ "Cannot infer both start and stop from an empty or all-null series; "
+ "provide explicit bounds."
+ )
label = kwargs.pop("label", info.label)
return slider(start=start, stop=stop, label=label, **kwargs)
@@ -552,9 +575,20 @@ class range_slider(UIElement[list[Numeric], Sequence[Numeric]]):
@staticmethod
def from_series(series: DataFrameSeries, **kwargs: Any) -> range_slider:
"""Create a range slider from a dataframe series."""
+ start = kwargs.pop("start", NO_VALUE)
+ stop = kwargs.pop("stop", NO_VALUE)
info = get_number_series_info(series)
- start = kwargs.pop("start", info.min)
- stop = kwargs.pop("stop", info.max)
+ if start is NO_VALUE:
+ start = info.min
+ if stop is NO_VALUE:
+ stop = info.max
+ if (start is NO_VALUE or start is None) and (
+ stop is NO_VALUE or stop is None
+ ):
+ raise ValueError(
+ "Cannot infer both start and stop from an empty or all-null series; "
+ "provide explicit bounds."
+ )
label = kwargs.pop("label", info.label)
return range_slider(start=start, stop=stop, label=label, **kwargs)
diff --git a/tests/_data/test_series.py b/tests/_data/test_series.py
index b7204ce747..f5859db535 100644
--- a/tests/_data/test_series.py
+++ b/tests/_data/test_series.py
@@ -6,13 +6,19 @@ from typing import Any
import pytest
from marimo._data.series import (
+ NumberSeriesInfo,
get_category_series_info,
get_date_series_info,
get_datetime_series_info,
get_number_series_info,
)
from marimo._dependencies.dependencies import DependencyManager
-from tests._data.mocks import NON_EAGER_LIBS, create_dataframes, create_series
+from tests._data.mocks import (
+ NON_EAGER_LIBS,
+ DFType,
+ create_dataframes,
+ create_series,
+)
HAS_DEPS = (
DependencyManager.pandas.has()
@@ -169,3 +175,67 @@ def is_pyarrow_type(response: Any) -> bool:
return isinstance(response, pa.Table)
except ImportError:
return False
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+ "df",
+ create_dataframes({"A": []}, exclude=NON_EAGER_LIBS),
+)
+def test_empty_series_info(df: Any) -> None:
+ series = df["A"]
+ number_info = get_number_series_info(series)
+ category_info = get_category_series_info(series)
+ date_info = get_date_series_info(series)
+ datetime_info = get_datetime_series_info(series)
+
+ assert number_info.min is None
+ assert number_info.max is None
+ assert category_info.categories == []
+ assert date_info.min is None
+ assert date_info.max is None
+ assert datetime_info.min is None
+ assert datetime_info.max is None
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+ "df",
+ create_dataframes({"A": [None, None]}, exclude=NON_EAGER_LIBS),
+)
+def test_all_null_series_info(df: Any) -> None:
+ series = df["A"]
+ assert get_number_series_info(series).min is None
+ assert get_number_series_info(series).max is None
+ assert get_category_series_info(series).categories == []
+ assert get_date_series_info(series).min is None
+ assert get_date_series_info(series).max is None
+ assert get_datetime_series_info(series).min is None
+ assert get_datetime_series_info(series).max is None
+
+
[email protected]("backend", ["pandas", "polars", "pyarrow"])
+ ("values", "minimum", "maximum"),
+ [
+ ([float("nan"), float("nan")], None, None),
+ ([None, float("nan")], None, None),
+ ([None, float("nan"), 1.0, 3.0], 1.0, 3.0),
+ ([float("nan"), float("inf")], float("inf"), float("inf")),
+ ([float("nan"), -float("inf")], -float("inf"), -float("inf")),
+ ([-float("inf"), float("inf")], -float("inf"), float("inf")),
+ ],
+)
+def test_number_series_with_nan_and_infinity(
+ backend: DFType,
+ values: list[float | None],
+ minimum: float | None,
+ maximum: float | None,
+) -> None:
+ pytest.importorskip(backend)
+ df = create_dataframes({"A": values}, include=[backend])[0]
+ assert get_number_series_info(df["A"]) == NumberSeriesInfo(
+ min=minimum,
+ max=maximum,
+ label="" if backend == "pyarrow" else "A",
+ )
diff --git a/tests/_plugins/ui/_impl/test_dates.py b/tests/_plugins/ui/_impl/test_dates.py
index 2b17d0dd12..4a40ebf438 100644
--- a/tests/_plugins/ui/_impl/test_dates.py
+++ b/tests/_plugins/ui/_impl/test_dates.py
@@ -316,6 +316,34 @@ def test_date_range_from_dataframe() -> None:
assert date_range_ui.stop == datetime.date(2024, 1, 31)
[email protected](not HAS_PANDAS, reason="pandas not installed")
[email protected]("widget", [ui.date, ui.datetime, ui.date_range])
+def test_date_from_empty_series(widget) -> None:
+ import pandas as pd
+
+ series = pd.Series([], dtype="datetime64[ns]")
+ with pytest.raises(ValueError, match="provide explicit bounds"):
+ widget.from_series(series)
+
+ if widget is ui.datetime:
+ start, stop = "2024-01-01T00:00", "2024-01-02T00:00"
+ else:
+ start, stop = "2024-01-01", "2024-01-02"
+ element = widget.from_series(series, start=start, stop=stop)
+ assert element.start.isoformat().startswith("2024-01-01")
+ assert element.stop.isoformat().startswith("2024-01-02")
+
+
[email protected](not HAS_PANDAS, reason="pandas not installed")
[email protected]("widget", [ui.date, ui.datetime, ui.date_range])
+def test_date_from_all_null_series(widget) -> None:
+ import pandas as pd
+
+ series = pd.Series([pd.NaT, pd.NaT], dtype="datetime64[ns]")
+ with pytest.raises(ValueError, match="provide explicit bounds"):
+ widget.from_series(series)
+
+
def test_datetime_precision_default() -> None:
"""Test that datetime precision defaults to 'minute'."""
dt = ui.datetime(value="2024-01-01T12:30:45")
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 6c2d70728d..d6ecfbdc7a 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -87,6 +87,59 @@ def test_number_from_dataframe() -> None:
assert number._args.label == "Custom label"
[email protected](not HAS_PANDAS, reason="pandas not installed")
[email protected]("widget", [ui.number, ui.slider, ui.range_slider])
+def test_numeric_from_empty_series(widget) -> None:
+ import pandas as pd
+
+ series = pd.Series([], dtype=float)
+ with pytest.raises(ValueError, match="provide explicit bounds"):
+ widget.from_series(series)
+
+ element = widget.from_series(series, start=0, stop=100)
+ assert element.start == 0
+ assert element.stop == 100
+
+
[email protected](not HAS_PANDAS, reason="pandas not installed")
[email protected]("widget", [ui.number, ui.slider, ui.range_slider])
+def test_numeric_from_all_null_series(widget) -> None:
+ import pandas as pd
+
+ series = pd.Series([None, None], dtype=float)
+ with pytest.raises(ValueError, match="provide explicit bounds"):
+ widget.from_series(series)
+
+
[email protected]("widget", [ui.number, ui.slider, ui.range_slider])
+ "values", [[float("nan"), float("nan")], [None, float("nan")]]
+)
+def test_numeric_from_pyarrow_nan_series(widget, values) -> None:
+ pa = pytest.importorskip("pyarrow")
+ series = pa.table({"x": values})["x"]
+
+ with pytest.raises(ValueError, match="provide explicit bounds"):
+ widget.from_series(series)
+
+ element = widget.from_series(series, start=0, stop=100)
+ assert element.start == 0
+ assert element.stop == 100
+
+
[email protected](not HAS_PANDAS, reason="pandas not installed")
[email protected]("widget", [ui.radio, ui.dropdown, ui.multiselect])
+def test_categorical_from_empty_series(widget) -> None:
+ import pandas as pd
+
+ series = pd.Series([], dtype=object)
+ element = widget.from_series(series)
+ assert element.options == {}
+
+ element = widget.from_series(series, options=["a", "b"])
+ assert element.options == {"a": "a", "b": "b"}
+
+
def test_slider_init() -> None:
slider = ui.slider(1, 10)
assert slider.start == 1