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.

feat: add lazy cache blob types (arrow, npy) (#9035)

Dylan Madisetti6baa22dcdced
diff --git a/marimo/_save/loaders/lazy.py b/marimo/_save/loaders/lazy.py
index cf56c3b30b..171e93b58a 100644
--- a/marimo/_save/loaders/lazy.py
+++ b/marimo/_save/loaders/lazy.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import pickle
import queue
import threading
+from enum import Enum, auto
from pathlib import Path
from typing import Any
@@ -18,11 +19,14 @@ from marimo._save.hash import HashKey
from marimo._save.loaders.loader import BasePersistenceLoader
from marimo._save.stores import FileStore, Store
from marimo._save.stubs import (
- LAZY_STUB_LOOKUP,
FunctionStub,
ModuleStub,
)
from marimo._save.stubs.lazy_stub import (
+ _LAZY_STUB_CACHE,
+ BLOB_DESERIALIZERS,
+ BLOB_SERIALIZERS,
+ LAZY_STUB_LOOKUP,
Cache as CacheSchema,
CacheType,
ImmediateReferenceStub,
@@ -30,10 +34,33 @@ from marimo._save.stubs.lazy_stub import (
Meta,
ReferenceStub,
)
+from marimo._save.stubs.stubs import mro_lookup
LOGGER = _loggers.marimo_logger()
+class _BlobStatus(Enum):
+ """Sentinel placed in the results queue when a blob is missing."""
+
+ MISSING = auto()
+
+
+def maybe_update_lazy_stub(value: Any) -> str:
+ """Return the loader strategy string for *value*, caching the result.
+
+ Walks the MRO of ``type(value)`` against ``LAZY_STUB_LOOKUP`` (a
+ fq-class-name → loader-string registry). Falls back to ``"pickle"``
+ when no match is found.
+ """
+ value_type = type(value)
+ if value_type in _LAZY_STUB_CACHE:
+ return _LAZY_STUB_CACHE[value_type]
+ result = mro_lookup(value_type, LAZY_STUB_LOOKUP)
+ loader = result[1] if result else "pickle"
+ _LAZY_STUB_CACHE[value_type] = loader
+ return loader
+
+
def to_item(
path: Path,
value: Any | None,
@@ -45,11 +72,27 @@ def to_item(
return Item()
if loader is None:
- loader = LAZY_STUB_LOOKUP.get(type(value), "pickle")
+ loader = maybe_update_lazy_stub(value)
+
+ type_hint = f"{type(value).__module__}.{type(value).__name__}"
if loader == "pickle":
return Item(
- reference=(path / f"{var_name}.pickle").as_posix(), hash=hash
+ reference=(path / f"{var_name}.pickle").as_posix(),
+ hash=hash,
+ type_hint=type_hint,
+ )
+ if loader == "npy":
+ return Item(
+ reference=(path / f"{var_name}.npy").as_posix(),
+ hash=hash,
+ type_hint=type_hint,
+ )
+ if loader == "arrow":
+ return Item(
+ reference=(path / f"{var_name}.arrow").as_posix(),
+ hash=hash,
+ type_hint=type_hint,
)
if loader == "ui":
return Item(reference=(path / "ui.pickle").as_posix())
@@ -60,7 +103,11 @@ def to_item(
if isinstance(value, (int, str, float, bool, bytes, type(None))):
return Item(primitive=value)
- return Item(reference=(path / f"{var_name}.pickle").as_posix(), hash=hash)
+ return Item(
+ reference=(path / f"{var_name}.pickle").as_posix(),
+ hash=hash,
+ type_hint=type_hint,
+ )
def from_item(item: Item) -> Any:
@@ -117,55 +164,72 @@ class LazyLoader(BasePersistenceLoader):
# Collect references to load
ref_vars: dict[str, str] = {}
+ ref_type_hints: dict[str, str | None] = {}
variable_hashes: dict[str, str] = {}
for var_name, item in cache_data.defs.items():
if var_name in cache_data.ui_defs:
ref_vars[var_name] = (base / "ui.pickle").as_posix()
elif item.reference is not None:
ref_vars[var_name] = item.reference
+ ref_type_hints[item.reference] = item.type_hint
if item.hash:
variable_hashes[var_name] = item.hash
# Eagerly resolve return value reference alongside defs
return_ref: str | None = None
+ return_type_hint: str | None = None
if (
cache_data.meta.return_value
and cache_data.meta.return_value.reference
):
return_ref = cache_data.meta.return_value.reference
+ return_type_hint = cache_data.meta.return_value.type_hint
- # Read + unpickle in parallel, stream results via queue
+ # Read + deserialize in parallel, stream results via queue.
+ # Every thread unconditionally puts exactly one item — either the
+ # deserialized value or _BlobStatus.MISSING — so queue.get() needs
+ # no timeout.
results: queue.Queue[tuple[str, Any]] = queue.Queue()
unique_keys = set(ref_vars.values())
if return_ref:
unique_keys.add(return_ref)
- def _load_and_unpickle(key: str) -> None:
- data = self.store.get(key)
- if data:
- results.put((key, pickle.loads(data)))
+ def _load_blob(key: str) -> None:
+ try:
+ data = self.store.get(key)
+ if data:
+ ext = Path(key).suffix
+ deserialize = BLOB_DESERIALIZERS.get(
+ ext, BLOB_DESERIALIZERS[".pickle"]
+ )
+ type_hint = ref_type_hints.get(key) or (
+ return_type_hint if key == return_ref else None
+ )
+ results.put((key, deserialize(data, type_hint)))
+ else:
+ results.put((key, _BlobStatus.MISSING))
+ except Exception as e:
+ LOGGER.warning("Failed to deserialize blob %s: %s", key, e)
+ results.put((key, _BlobStatus.MISSING))
threads = [
- threading.Thread(target=_load_and_unpickle, args=(key,))
+ threading.Thread(target=_load_blob, args=(key,))
for key in unique_keys
]
for t in threads:
t.start()
- # Stream results as they arrive
+ # N threads → N results guaranteed; no timeout needed.
unpickled: dict[str, Any] = {}
- for _ in unique_keys:
- try:
- key, val = results.get(timeout=30)
+ try:
+ for _ in unique_keys:
+ key, val = results.get()
+ if val is _BlobStatus.MISSING:
+ raise FileNotFoundError("Incomplete cache: missing blobs")
unpickled[key] = val
- except queue.Empty:
- break
-
- for t in threads:
- t.join()
-
- if len(unpickled) < len(unique_keys):
- raise FileNotFoundError("Incomplete cache: missing blobs")
+ finally:
+ for t in threads:
+ t.join()
# Distribute to defs
defs: dict[str, Any] = {}
@@ -210,25 +274,28 @@ class LazyLoader(BasePersistenceLoader):
path, cache.meta.get("return", None), var_name="return"
)
if return_item.reference:
- return_item.reference = (path / "return.pickle").as_posix()
+ # Normalize base name to "return" while preserving format extension.
+ ext = Path(return_item.reference).suffix
+ return_item.reference = (path / f"return{ext}").as_posix()
try:
cache_type_enum = CacheType(cache.cache_type)
except ValueError:
cache_type_enum = CacheType.UNKNOWN
- pickle_vars: dict[str, Any] = {}
+ # Separate vars by loader strategy
+ format_vars: dict[str, dict[str, Any]] = {} # loader → {var: obj}
ui_vars: dict[str, Any] = {}
defs_dict: dict[str, Item] = {}
ui_defs_list: list[str] = []
for var, obj in cache.defs.items():
- loader = LAZY_STUB_LOOKUP.get(type(obj), "pickle")
- if loader == "pickle":
- pickle_vars[var] = obj
- elif loader == "ui":
+ loader = maybe_update_lazy_stub(obj)
+ if loader == "ui":
ui_vars[var] = obj
ui_defs_list.append(var)
+ elif loader not in ("inline",):
+ format_vars.setdefault(loader, {})[var] = obj
defs_dict[var] = to_item(
path,
obj,
@@ -255,23 +322,33 @@ class LazyLoader(BasePersistenceLoader):
store = self.store
return_ref = return_item.reference
return_value = cache.meta.get("return", None)
+ return_loader = (
+ maybe_update_lazy_stub(return_value)
+ if return_value is not None
+ else "pickle"
+ )
manifest_key = str(self.build_path(cache.key))
def _serialize_and_write() -> None:
"""Serialize and write all blobs + manifest in background."""
try:
if return_ref:
- store.put(return_ref, pickle.dumps(return_value))
+ serialize = BLOB_SERIALIZERS.get(
+ return_loader, pickle.dumps
+ )
+ store.put(return_ref, serialize(return_value))
if ui_vars:
store.put(
(path / "ui.pickle").as_posix(),
pickle.dumps(ui_vars),
)
- for var, obj in pickle_vars.items():
- store.put(
- (path / f"{var}.pickle").as_posix(),
- pickle.dumps(obj),
- )
+ for loader, vars_dict in format_vars.items():
+ serialize = BLOB_SERIALIZERS.get(loader, pickle.dumps)
+ for var, obj in vars_dict.items():
+ store.put(
+ (path / f"{var}.{loader}").as_posix(),
+ serialize(obj),
+ )
# Manifest last — readers check for it to detect complete writes
store.put(manifest_key, manifest)
except Exception:
diff --git a/marimo/_save/stubs/__init__.py b/marimo/_save/stubs/__init__.py
index b3205f22c1..d8b759b83b 100644
--- a/marimo/_save/stubs/__init__.py
+++ b/marimo/_save/stubs/__init__.py
@@ -6,12 +6,13 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any
from marimo._save.stubs.function_stub import FunctionStub
-from marimo._save.stubs.lazy_stub import ReferenceStub
+from marimo._save.stubs.lazy_stub import LAZY_STUB_LOOKUP, ReferenceStub
from marimo._save.stubs.module_stub import ModuleStub
from marimo._save.stubs.pydantic_stub import PydanticStub
from marimo._save.stubs.stubs import (
CUSTOM_STUBS,
CustomStub,
+ mro_lookup,
register_stub,
)
from marimo._save.stubs.ui_element_stub import UIElementStub
@@ -27,18 +28,6 @@ STUB_REGISTRATIONS: dict[str, Callable[[Any], None]] = {
"pydantic.main.BaseModel": PydanticStub.register,
}
-LAZY_STUB_LOOKUP: dict[type, str] = {
- int: "inline",
- str: "inline",
- float: "inline",
- bool: "inline",
- bytes: "inline",
- type(None): "inline",
- FunctionStub: "inline",
- ModuleStub: "inline",
- UIElementStub: "ui",
-}
-
def maybe_register_stub(value: Any) -> bool:
"""Lazily register a stub for a value's type if not already registered.
@@ -57,25 +46,13 @@ def maybe_register_stub(value: Any) -> bool:
if value_type in CUSTOM_STUBS:
return True
- # Walk MRO to find matching base class
- try:
- mro_list = value_type.mro()
- except BaseException:
- # Some exotic metaclasses or broken types may raise when calling mro
- mro_list = [value_type]
-
- for cls in mro_list:
- if not (hasattr(cls, "__module__") and hasattr(cls, "__name__")):
- continue
-
- cls_name = f"{cls.__module__}.{cls.__name__}"
-
- if cls_name in STUB_REGISTRATIONS:
- if cls_name not in _REGISTERED_NAMES:
- _REGISTERED_NAMES.add(cls_name)
- STUB_REGISTRATIONS[cls_name](value)
- # After registration attempt, check if now in CUSTOM_STUBS
- return value_type in CUSTOM_STUBS
+ result = mro_lookup(value_type, STUB_REGISTRATIONS)
+ if result is not None:
+ cls_name, register_fn = result
+ if cls_name not in _REGISTERED_NAMES:
+ _REGISTERED_NAMES.add(cls_name)
+ register_fn(value)
+ return value_type in CUSTOM_STUBS
return False
@@ -107,5 +84,6 @@ __all__ = [
"UIElementStub",
"maybe_get_custom_stub",
"maybe_register_stub",
+ "mro_lookup",
"register_stub",
]
diff --git a/marimo/_save/stubs/lazy_stub.py b/marimo/_save/stubs/lazy_stub.py
index b5dc523491..c64b2d3974 100644
--- a/marimo/_save/stubs/lazy_stub.py
+++ b/marimo/_save/stubs/lazy_stub.py
@@ -1,17 +1,22 @@
# Copyright 2026 Marimo. All rights reserved.
-"""msgspec schemas for lazy cache serialization."""
+"""msgspec schemas and format registries for lazy cache serialization."""
from __future__ import annotations
+import io
import pickle
from enum import Enum
-from typing import Any
+from typing import TYPE_CHECKING, Any
import msgspec
+from marimo._dependencies.dependencies import DependencyManager
from marimo._save.stores import get_store
from marimo._save.stubs.stubs import CustomStub
+if TYPE_CHECKING:
+ from collections.abc import Callable
+
class CacheType(Enum):
CONTEXT_EXECUTION_PATH = "ContextExecutionPath"
@@ -34,6 +39,9 @@ class Item(msgspec.Struct):
# (code, filename, linenumber)
function: tuple[str, str, int] | None = None
hash: str | None = None
+ # Fully-qualified class name of the original value — used by format-aware
+ # deserializers (e.g. to distinguish pandas from polars Arrow blobs).
+ type_hint: str | None = None
def __post_init__(self) -> None:
fields_set = sum(
@@ -64,6 +72,143 @@ class Cache(msgspec.Struct):
ui_defs: list[str] = msgspec.field(default_factory=list)
+# ---------------------------------------------------------------------------
+# Unified type-to-loader registry
+# ---------------------------------------------------------------------------
+
+# Maps fully-qualified class names to loader strategy strings.
+# String keys mean optional deps (numpy, polars, pandas) are never imported at
+# module load time — they are only imported when the serializer/deserializer
+# is actually called.
+#
+# Strategies:
+# "inline" — stored as a primitive/function/module field in the Item struct
+# "ui" — shared ui.pickle blob (UIElement family)
+# "pickle" — per-variable .pickle blob (default fallback)
+# "npy" — numpy .npy blob
+# "arrow" — Arrow IPC .arrow blob (polars and pandas)
+LAZY_STUB_LOOKUP: dict[str, str] = {
+ "builtins.int": "inline",
+ "builtins.str": "inline",
+ "builtins.float": "inline",
+ "builtins.bool": "inline",
+ "builtins.bytes": "inline",
+ "builtins.NoneType": "inline",
+ "marimo._save.stubs.function_stub.FunctionStub": "inline",
+ "marimo._save.stubs.module_stub.ModuleStub": "inline",
+ "marimo._save.stubs.ui_element_stub.UIElementStub": "ui",
+ # Optional third-party types — imported lazily only when encountered:
+ "numpy.ndarray": "npy",
+ "polars.dataframe.frame.DataFrame": "arrow",
+ "polars.series.series.Series": "arrow",
+ # pandas 3.x sets __module__ = "pandas"; 2.x exposes the internal path
+ "pandas.DataFrame": "arrow",
+ "pandas.core.frame.DataFrame": "arrow",
+ "pandas.Series": "arrow",
+ "pandas.core.series.Series": "arrow",
+}
+
+# Runtime cache: type → loader string, populated by maybe_update_lazy_stub().
+_LAZY_STUB_CACHE: dict[type, str] = {}
+
+# ---------------------------------------------------------------------------
+# Deserializers — keyed by file extension
+# ---------------------------------------------------------------------------
+
+
+def _npy_load(data: bytes, type_hint: str | None = None) -> Any:
+ DependencyManager.numpy.require("to load cached numpy arrays.")
+ import numpy as np
+
+ del type_hint
+ return np.load(io.BytesIO(data), allow_pickle=True)
+
+
+def _arrow_load(data: bytes, type_hint: str | None = None) -> Any:
+ # type_hint is the fq class name written by to_item() at save time.
+ # Using it (rather than schema metadata inspection) is explicit and
+ # version-stable across pyarrow/polars/pandas releases.
+ DependencyManager.pyarrow.require("to load cached Arrow IPC blobs.")
+ import pyarrow as pa
+
+ reader = pa.ipc.open_file(io.BytesIO(data))
+ table = reader.read_all()
+ if type_hint and type_hint.startswith("pandas."):
+ df = table.to_pandas()
+ if type_hint in ("pandas.Series", "pandas.core.series.Series"):
+ # Stored as a single-column DataFrame; recover as a Series.
+ return df.iloc[:, 0]
+ return df
+ import polars as pl
+
+ result = pl.from_arrow(table)
+ if type_hint == "polars.series.series.Series":
+ # Stored as a single-column DataFrame; recover as a Series.
+ if isinstance(result, pl.DataFrame):
+ return result.to_series(0)
+ return result
+ return result
+
+
+def _pickle_load(data: bytes, type_hint: str | None = None) -> Any:
+ del type_hint
+ return pickle.loads(data)
+
+
+BLOB_DESERIALIZERS: dict[str, Callable[[bytes, str | None], Any]] = {
+ ".pickle": _pickle_load,
+ ".npy": _npy_load,
+ ".arrow": _arrow_load,
+}
+
+# ---------------------------------------------------------------------------
+# Serializers — keyed by loader strategy string
+# ---------------------------------------------------------------------------
+
+
+def _npy_dump(obj: Any) -> bytes:
+ DependencyManager.numpy.require("to save numpy arrays to cache.")
+ import numpy as np
+
+ buf = io.BytesIO()
+ np.save(buf, obj)
+ return buf.getvalue()
+
+
+def _arrow_dump(obj: Any) -> bytes:
+ # Duck-type dispatch:
+ # polars DataFrame → write_ipc()
+ # pandas DataFrame → to_feather()
+ # Series (either) → to_frame() first, then the appropriate DataFrame method
+ # Fall back to pickle when pyarrow is absent so the cache write never fails.
+ if not DependencyManager.pyarrow.has():
+ return pickle.dumps(obj)
+ buf = io.BytesIO()
+ if hasattr(obj, "write_ipc"): # polars DataFrame
+ obj.write_ipc(buf)
+ elif hasattr(obj, "to_feather"): # pandas DataFrame
+ obj.to_feather(buf)
+ else:
+ # Series — promote to single-column DataFrame, then detect library
+ frame = obj.to_frame()
+ if hasattr(frame, "write_ipc"): # polars Series → polars DataFrame
+ frame.write_ipc(buf)
+ else: # pandas Series → pandas DataFrame
+ frame.to_feather(buf)
+ return buf.getvalue()
+
+
+BLOB_SERIALIZERS: dict[str, Callable[[Any], bytes]] = {
+ "pickle": pickle.dumps,
+ "npy": _npy_dump,
+ "arrow": _arrow_dump,
+}
+
+# ---------------------------------------------------------------------------
+# Stubs for deferred / immediate blob loading
+# ---------------------------------------------------------------------------
+
+
class ReferenceStub:
"""Deferred blob reference — loads from store on access."""
diff --git a/marimo/_save/stubs/stubs.py b/marimo/_save/stubs/stubs.py
index 7aca0f326d..d8f39a06f9 100644
--- a/marimo/_save/stubs/stubs.py
+++ b/marimo/_save/stubs/stubs.py
@@ -4,9 +4,33 @@
from __future__ import annotations
import abc
-from typing import Any
+from typing import Any, TypeVar
-__all__ = ["CUSTOM_STUBS", "CustomStub", "register_stub"]
+__all__ = ["CUSTOM_STUBS", "CustomStub", "mro_lookup", "register_stub"]
+
+_T = TypeVar("_T")
+
+
+def mro_lookup(
+ value_type: type, registry: dict[str, _T]
+) -> tuple[str, _T] | None:
+ """Walk the MRO of value_type, return ``(fq_name, value)`` for first match.
+
+ Matches against fully-qualified class names of the form
+ ``"{cls.__module__}.{cls.__name__}"``. Returns ``None`` if no class in
+ the MRO is found in *registry*.
+ """
+ try:
+ mro_list = value_type.mro()
+ except Exception:
+ mro_list = [value_type]
+ for cls in mro_list:
+ if not (hasattr(cls, "__module__") and hasattr(cls, "__name__")):
+ continue
+ key = f"{cls.__module__}.{cls.__name__}"
+ if key in registry:
+ return key, registry[key]
+ return None
class CustomStub(abc.ABC):
diff --git a/tests/_save/loaders/test_loader.py b/tests/_save/loaders/test_loader.py
index af600b6cb7..cde916b69e 100644
--- a/tests/_save/loaders/test_loader.py
+++ b/tests/_save/loaders/test_loader.py
@@ -10,6 +10,7 @@ from pathlib import Path
import msgspec
import pytest
+from marimo._dependencies.dependencies import DependencyManager
from marimo._save.cache import MARIMO_CACHE_VERSION, Cache
from marimo._save.hash import HashKey
from marimo._save.loaders import (
@@ -286,3 +287,164 @@ class TestLazyLoader(ABCTestLoader):
result = loader.load_cache(key("missing", "Pure"))
assert result is None
+
+ @pytest.mark.skipif(
+ not DependencyManager.numpy.has(), reason="numpy required"
+ )
+ def test_numpy_object_dtype_round_trip(self) -> None:
+ """Object-dtype numpy arrays survive save → flush → load via .npy."""
+ import numpy as np
+
+ loader = self.instance()
+ arr = np.array(["a", "b"], dtype=object)
+ cache = Cache(
+ defs={"arr": arr},
+ hash="np_obj_hash",
+ cache_type="Pure",
+ stateful_refs=set(),
+ hit=False,
+ meta={"version": MARIMO_CACHE_VERSION},
+ )
+ assert loader.save_cache(cache)
+ loader.flush()
+
+ loaded = loader.load_cache(key("np_obj_hash", "Pure"))
+ assert loaded is not None
+ np.testing.assert_array_equal(loaded.defs["arr"], arr)
+
+ @pytest.mark.skipif(
+ not DependencyManager.numpy.has(), reason="numpy required"
+ )
+ def test_numpy_round_trip(self) -> None:
+ """numpy arrays survive save → flush → load via .npy format."""
+ import numpy as np
+
+ loader = self.instance()
+ arr = np.array([1.0, 2.0, 3.0])
+ cache = Cache(
+ defs={"arr": arr},
+ hash="np_hash",
+ cache_type="Pure",
+ stateful_refs=set(),
+ hit=False,
+ meta={"version": MARIMO_CACHE_VERSION},
+ )
+ assert loader.save_cache(cache)
+ loader.flush()
+
+ assert list(Path(self.store.save_path).rglob("*.npy")), (
+ "expected .npy blob, got pickle fallback"
+ )
+ loaded = loader.load_cache(key("np_hash", "Pure"))
+ assert loaded is not None
+ np.testing.assert_array_equal(loaded.defs["arr"], arr)
+
+ @pytest.mark.skipif(
+ not DependencyManager.has("polars"), reason="polars required"
+ )
+ def test_polars_round_trip(self) -> None:
+ """polars DataFrames survive save → flush → load via .arrow format."""
+ import polars as pl
+
+ loader = self.instance()
+ df = pl.DataFrame({"a": [1, 2], "b": [3.0, 4.0]})
+ cache = Cache(
+ defs={"df": df},
+ hash="pl_hash",
+ cache_type="Pure",
+ stateful_refs=set(),
+ hit=False,
+ meta={"version": MARIMO_CACHE_VERSION},
+ )
+ assert loader.save_cache(cache)
+ loader.flush()
+
+ assert list(Path(self.store.save_path).rglob("*.arrow")), (
+ "expected .arrow blob, got pickle fallback"
+ )
+ loaded = loader.load_cache(key("pl_hash", "Pure"))
+ assert loaded is not None
+ assert loaded.defs["df"].equals(df)
+
+ @pytest.mark.skipif(
+ not DependencyManager.has("polars"), reason="polars required"
+ )
+ def test_polars_series_round_trip(self) -> None:
+ """polars Series survive save → flush → load via .arrow format."""
+ import polars as pl
+
+ loader = self.instance()
+ s = pl.Series("vals", [10, 20, 30])
+ cache = Cache(
+ defs={"s": s},
+ hash="pl_series_hash",
+ cache_type="Pure",
+ stateful_refs=set(),
+ hit=False,
+ meta={"version": MARIMO_CACHE_VERSION},
+ )
+ assert loader.save_cache(cache)
+ loader.flush()
+
+ assert list(Path(self.store.save_path).rglob("*.arrow")), (
+ "expected .arrow blob, got pickle fallback"
+ )
+ loaded = loader.load_cache(key("pl_series_hash", "Pure"))
+ assert loaded is not None
+ assert isinstance(loaded.defs["s"], pl.Series)
+ assert loaded.defs["s"].to_list() == s.to_list()
+
+ @pytest.mark.skipif(
+ not DependencyManager.has("pandas"), reason="pandas required"
+ )
+ def test_pandas_round_trip(self) -> None:
+ """pandas DataFrames survive save → flush → load via .arrow format."""
+ import pandas as pd
+
+ loader = self.instance()
+ df = pd.DataFrame({"x": [1, 2], "y": [3.0, 4.0]})
+ cache = Cache(
+ defs={"df": df},
+ hash="pd_hash",
+ cache_type="Pure",
+ stateful_refs=set(),
+ hit=False,
+ meta={"version": MARIMO_CACHE_VERSION},
+ )
+ assert loader.save_cache(cache)
+ loader.flush()
+
+ assert list(Path(self.store.save_path).rglob("*.arrow")), (
+ "expected .arrow blob, got pickle fallback"
+ )
+ loaded = loader.load_cache(key("pd_hash", "Pure"))
+ assert loaded is not None
+ pd.testing.assert_frame_equal(loaded.defs["df"], df)
+
+ @pytest.mark.skipif(
+ not DependencyManager.has("pandas"), reason="pandas required"
+ )
+ def test_pandas_series_round_trip(self) -> None:
+ """pandas Series survive save → flush → load via .arrow format."""
+ import pandas as pd
+
+ loader = self.instance()
+ s = pd.Series([10, 20, 30], name="vals")
+ cache = Cache(
+ defs={"s": s},
+ hash="pd_series_hash",
+ cache_type="Pure",
+ stateful_refs=set(),
+ hit=False,
+ meta={"version": MARIMO_CACHE_VERSION},
+ )
+ assert loader.save_cache(cache)
+ loader.flush()
+
+ assert list(Path(self.store.save_path).rglob("*.arrow")), (
+ "expected .arrow blob, got pickle fallback"
+ )
+ loaded = loader.load_cache(key("pd_series_hash", "Pure"))
+ assert loaded is not None
+ assert isinstance(loaded.defs["s"], pd.Series)
+ pd.testing.assert_series_equal(loaded.defs["s"], s)