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: data_editor corrupting int8/uint8/float16 columns on edit (#10211)

Madan kumar33b3a4d05f8a
diff --git a/marimo/_plugins/ui/_impl/data_editor.py b/marimo/_plugins/ui/_impl/data_editor.py
index 3a8e17beb6..e74406f808 100644
--- a/marimo/_plugins/ui/_impl/data_editor.py
+++ b/marimo/_plugins/ui/_impl/data_editor.py
@@ -339,16 +339,9 @@ def _convert_value(
return datetime.date.fromisoformat(value)
elif dtype == nw.Duration:
return datetime.timedelta(microseconds=float(value))
- elif dtype == nw.Float32 or dtype == nw.Float64:
+ elif hasattr(dtype, "is_float") and dtype.is_float():
return float(value)
- elif (
- dtype == nw.Int16
- or dtype == nw.Int32
- or dtype == nw.Int64
- or dtype == nw.UInt16
- or dtype == nw.UInt32
- or dtype == nw.UInt64
- ):
+ elif hasattr(dtype, "is_integer") and dtype.is_integer():
return int(value)
elif (
dtype == nw.String
diff --git a/tests/_plugins/ui/_impl/test_data_editor.py b/tests/_plugins/ui/_impl/test_data_editor.py
index 101562bb32..d375556426 100644
--- a/tests/_plugins/ui/_impl/test_data_editor.py
+++ b/tests/_plugins/ui/_impl/test_data_editor.py
@@ -353,6 +353,40 @@ def test_apply_edits_dataframe():
assert pd.DataFrame({"A": [1, 2, 3], "B": ["a", "x", "c"]}).equals(result)
+ not DependencyManager.pandas.has(), reason="Pandas not installed"
+)
[email protected]("dtype", ["int8", "uint8", "float16"])
+def test_apply_edits_dataframe_small_width_numeric_preserves_dtype(dtype):
+ # Editing a cell must not coerce a narrow numeric column to object/string.
+ import pandas as pd
+
+ df = pd.DataFrame({"A": pd.array([1, 2, 3], dtype=dtype)})
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "A", "value": "5"}]
+ }
+ result = apply_edits(df.copy(), edits)
+ assert pd.api.types.is_numeric_dtype(result["A"])
+ assert result["A"].tolist() == [5, 2, 3]
+
+
+ not DependencyManager.polars.has(), reason="Polars not installed"
+)
+def test_apply_edits_dataframe_polars_int8_preserves_dtype():
+ import polars as pl
+
+ df = pl.DataFrame({"A": pl.Series([1, 2, 3], dtype=pl.Int8)})
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "A", "value": "5"}]
+ }
+ result = apply_edits(df.clone(), edits)
+ # The column stays integer (not coerced to String) even though the
+ # column-oriented round-trip widens the exact type to Int64.
+ assert result["A"].dtype.is_integer()
+ assert result["A"].to_list() == [5, 2, 3]
+
+
@pytest.mark.skipif(
not DependencyManager.polars.has(), reason="Polars not installed"
)
@@ -963,6 +997,24 @@ class TestConvertValue:
assert result == 42
assert isinstance(result, int)
+ @pytest.mark.parametrize(
+ ("dtype", "value", "expected"),
+ [
+ (nw.Int8, "42", 42),
+ (nw.UInt8, "42", 42),
+ (nw.Int128, "42", 42),
+ (nw.UInt128, "42", 42),
+ # Float16 where narwhals exposes it, else any float width.
+ (getattr(nw, "Float16", nw.Float32), "3.5", 3.5),
+ ],
+ )
+ def test_convert_value_covers_narrow_numeric_widths(
+ self, dtype, value, expected
+ ):
+ result = _convert_value(value, None, dtype)
+ assert result == expected
+ assert isinstance(result, type(expected))
+
def test_convert_value_with_dtype_string(self):
"""Test String conversion with dtype."""
result = _convert_value(42, None, nw.String)