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: unify Polars numeric checks and restore cache version 5 (#10816)

Péter Gyarmati386ada0d7728
diff --git a/marimo/_runtime/primitives.py b/marimo/_runtime/primitives.py
index 5f8e911044..72e98ded98 100644
--- a/marimo/_runtime/primitives.py
+++ b/marimo/_runtime/primitives.py
@@ -76,28 +76,26 @@ def is_data_primitive(value: Any) -> bool:
if is_instance_by_name(value, "torch.Tensor"):
return str(value.device) == "cpu"
- # Series follow the same numeric-schema policy as dataframes.
- if is_instance_by_name(value, "polars.series.series.Series"):
- return bool(value.dtype.is_numeric())
-
+ is_polars = type(value).__module__.startswith("polars.")
# If a numpy like array, ensure that it's not an object array.
- if hasattr(value, "dtype"):
+ if hasattr(value, "dtype") and not is_polars:
return not (
value.dtype is None
or (hasattr(value.dtype, "hasobject") and value.dtype.hasobject)
)
- elif hasattr(value, "dtypes"):
+ elif hasattr(value, "dtypes") or is_polars:
# Bit of discrepancy between objects like polars and pandas, so use
# narwhals to normalize the dataframe.
import narwhals.stable.v2 as nw
try:
+ frame = nw.from_native(value, allow_series=True)
+ if isinstance(frame, nw.Series):
+ frame = frame.to_frame()
return bool(
all(
dtype.is_numeric()
- for dtype in nw.from_native(value)
- .collect_schema()
- .dtypes()
+ for dtype in frame.collect_schema().dtypes()
)
)
except Exception:
diff --git a/tests/_runtime/test_primitives.py b/tests/_runtime/test_primitives.py
index 83cfad002f..3fbe6d9a50 100644
--- a/tests/_runtime/test_primitives.py
+++ b/tests/_runtime/test_primitives.py
@@ -27,7 +27,8 @@ class TestDataPrimitiveClassification:
@pytest.mark.skipif(
not DependencyManager.polars.has(), reason="polars required"
)
- def test_polars_series_numeric_classification(self) -> None:
+ @pytest.mark.parametrize("as_frame", [False, True])
+ def test_polars_numeric_classification(self, as_frame: bool) -> None:
import polars as pl
values = [
@@ -37,7 +38,12 @@ class TestDataPrimitiveClassification:
pl.Series("list", [[1], [2]]),
pl.Series("object", [object()], dtype=pl.Object),
]
- assert {value.name: is_data_primitive(value) for value in values} == {
+ assert {
+ value.name: is_data_primitive(
+ value.to_frame() if as_frame else value
+ )
+ for value in values
+ } == {
"integer": True,
"float": True,
"string": False,
diff --git a/tests/_save/loaders/test_loader.py b/tests/_save/loaders/test_loader.py
index 40d6a0b415..fac492106e 100644
--- a/tests/_save/loaders/test_loader.py
+++ b/tests/_save/loaders/test_loader.py
@@ -611,7 +611,7 @@ class TestLazyLoader(ABCTestLoader):
@pytest.mark.skipif(
not DependencyManager.has("polars"), reason="polars required"
)
- @pytest.mark.parametrize("stored_version", [None, 5])
+ @pytest.mark.parametrize("stored_version", [None, 5, 6])
def test_polars_cache_versions_reuse_values(
self, stored_version: int | None
) -> None:
@@ -632,7 +632,9 @@ class TestLazyLoader(ABCTestLoader):
restored = loader.cache_attempt({"frame"}, cache.key, set())
assert restored.hit
assert restored.defs["frame"].equals(frame)
- assert restored.meta["version"] == (6 if stored_version is None else 5)
+ assert restored.meta["version"] == (
+ MARIMO_CACHE_VERSION if stored_version is None else stored_version
+ )
@pytest.mark.skipif(
not DependencyManager.has("pandas"), reason="pandas required"