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.

chore: harden df numeric checking (#9086)

Dylan Madisettic0b1f81fe17d
diff --git a/marimo/_runtime/primitives.py b/marimo/_runtime/primitives.py
index 8dd7051b7c..4dffec90bd 100644
--- a/marimo/_runtime/primitives.py
+++ b/marimo/_runtime/primitives.py
@@ -87,18 +87,15 @@ def is_data_primitive(value: Any) -> bool:
try:
return bool(
- nw.narwhalify(
- lambda df: all(
- df[col].dtype.is_numeric() for col in df.columns
- )
- )(value)
- )
- except Exception as err:
- raise err from ValueError(
- "Unexpected datatype, narwhals was unable to normalize "
- "dataframe. Please report this to "
- "github.com/marimo-team/marimo"
+ all(
+ dtype.is_numeric()
+ for dtype in nw.from_native(value)
+ .collect_schema()
+ .dtypes()
+ )
)
+ except Exception:
+ return False
# Otherwise may be a closely related array object
return True
diff --git a/marimo/_save/encode.py b/marimo/_save/encode.py
index d109a13f54..b62930482f 100644
--- a/marimo/_save/encode.py
+++ b/marimo/_save/encode.py
@@ -166,8 +166,9 @@ def deterministic_dumps(obj: Any, hash_type: str) -> bytes:
h = hashlib.new(hash_type, usedforsecurity=False)
h.update(_contiguous_tensor_bytes(obj))
return (bytes, (h.digest(),))
- except (TypeError, ValueError):
+ except Exception:
pass
+ # Falls back to parent pickle
return NotImplemented
buf = io.BytesIO()
diff --git a/tests/_save/test_encode.py b/tests/_save/test_encode.py
index 958c9cae23..3599fcec8d 100644
--- a/tests/_save/test_encode.py
+++ b/tests/_save/test_encode.py
@@ -6,8 +6,14 @@ import array
import pickle
from typing import Any
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
from marimo._save.encode import deterministic_dumps
+HAS_PANDAS = DependencyManager.pandas.has()
+HAS_NUMPY = DependencyManager.numpy.has()
+
class _ArrayWithSet:
"""Array-like whose pickle is non-deterministic across processes.
@@ -79,3 +85,40 @@ def test_0d_array() -> None:
det1 = deterministic_dumps(obj, "sha256")
det2 = deterministic_dumps(obj, "sha256")
assert det1 == det2
+
+
+ not HAS_PANDAS or not HAS_NUMPY,
+ reason="pandas and numpy are required",
+)
+def test_dataframe_with_integer_columns() -> None:
+ """Regression: pd.DataFrame(np.random.randn(3, 3)) has integer column names.
+
+ narwhals interprets df[0] as row selection, not column selection, causing
+ AttributeError: 'DataFrame' object has no attribute 'dtype'.
+ """
+ import numpy as np
+ import pandas as pd
+
+ df = pd.DataFrame(np.random.randn(3, 3))
+ # Must not raise
+ result = deterministic_dumps(df, hash_type="sha256")
+ assert isinstance(result, bytes)
+
+
+ not HAS_PANDAS or not HAS_NUMPY,
+ reason="pandas and numpy are required",
+)
+def test_dataframe_same_data_same_hash() -> None:
+ """Two DataFrames with identical data must produce the same hash."""
+ import numpy as np
+ import pandas as pd
+
+ data = np.arange(9, dtype=float).reshape(3, 3)
+ df1 = pd.DataFrame(data)
+ df2 = pd.DataFrame(data.copy())
+
+ assert deterministic_dumps(df1, hash_type="sha256") == deterministic_dumps(
+ df2, hash_type="sha256"
+ )