Skip to content

fix: data_editor corrupts float16 columns on edit (follow-up to #10211) - #10304

Merged
mscolnick merged 1 commit into
mainfrom
fix/data-editor-float16-corruption
Jul 23, 2026
Merged

mscolnick merged 1 commit into
mainfrom
fix/data-editor-float16-corruption

Conversation

@mscolnick

Copy link
Copy Markdown
Contributor

This pull request was authored by a coding agent.

What / why

Editing a cell in a pandas float16 column silently corrupts the whole column: the edited value is stored as a string and the column dtype flips to object.

import pandas as pd
from marimo._plugins.ui._impl.data_editor import apply_edits

df = pd.DataFrame({"a": pd.array([1.0, 2.0, 3.0], dtype="float16")})
apply_edits(df, {"edits": [{"rowIdx": 0, "columnId": "a", "value": "5.5"}]})
# before: dtype object, values ['5.5', 2.0, 3.0]   (corrupted)
# after:  dtype float16-compatible numeric, values [5.5, 2.0, 3.0]

#10211 addressed int8/uint8/float32 by switching to dtype.is_integer() / dtype.is_float(), but float16 still degrades: narwhals (2.x) has no Float16, so it reports pandas float16 as nw.Unknown. Both predicate checks return False and _convert_value falls through to return str(value). The dtype-preservation test the PR added for float16 is currently red on main:

FAILED tests/_plugins/ui/_impl/test_data_editor.py::test_apply_edits_dataframe_small_width_numeric_preserves_dtype[float16]

Fix

For an unsupported/Unknown dtype, fall through to the existing original-value coercion (_convert_value already handles int/float/str/…) instead of stringifying. For a numeric column the original value is a Python float/int, so the edit stays numeric. This is a superset fix — any dtype narwhals can't classify but whose values are numeric now round-trips correctly.

Tests

  • The previously-failing test_apply_edits_dataframe_small_width_numeric_preserves_dtype[float16] now passes.
  • Added test_convert_value_unknown_dtype_coerces_from_original, which pins the fall-through directly against nw.Unknown (independent of narwhals' evolving float16 mapping).
  • pytest tests/_plugins/ui/_impl/test_data_editor.py → 107 passed; ruff check/ruff format clean.

Follow-up to #10211.

… columns on edit

narwhals maps some pandas extension dtypes (notably float16) to nw.Unknown,
so _convert_value fell through to `str(value)`, coercing the whole column to
object. #10211 fixed int8/uint8/float32 but float16 still degraded — its own
dtype-preservation test was red on main.

Fall through to the existing original-value coercion for Unknown dtypes
instead of stringifying, keeping numeric columns numeric.
@vercel

vercel Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Jul 23, 2026 6:12pm

Request Review

@mscolnick
mscolnick marked this pull request as ready for review July 23, 2026 18:13
Copilot AI review requested due to automatic review settings July 23, 2026 18:13
@mscolnick mscolnick added the bug Something isn't working label Jul 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +369 to 378
# narwhals maps some pandas extension dtypes (notably
# float16) to nw.Unknown. Stringifying here would coerce the
# whole column to object; fall through to the original-value
# coercion below, which keeps the column numeric.
LOGGER.debug(
"Unhandled dtype %s; coercing from original value", dtype
)

if original_value is None:
return value
Comment on lines +1018 to +1027
def test_convert_value_unknown_dtype_coerces_from_original(self):
"""An Unknown dtype (e.g. pandas float16) must not stringify.

narwhals reports some pandas extension dtypes as nw.Unknown; the
conversion should fall back to the original value's type instead of
coercing the column to object/string.
"""
result = _convert_value("5.5", 1.0, nw.Unknown)
assert result == 5.5
assert isinstance(result, float)

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@mscolnick
mscolnick merged commit ac5f44e into main Jul 23, 2026
45 of 46 checks passed
@mscolnick
mscolnick deleted the fix/data-editor-float16-corruption branch July 23, 2026 19:02
williambdean pushed a commit to williambdean/marimo that referenced this pull request Sep 6, 2026
…o-team#10211) (marimo-team#10304)

**This pull request was authored by a coding agent.**

## What / why

Editing a cell in a **pandas `float16`** column silently corrupts the
whole column: the edited value is stored as a string and the column
dtype flips to `object`.

```python
import pandas as pd
from marimo._plugins.ui._impl.data_editor import apply_edits

df = pd.DataFrame({"a": pd.array([1.0, 2.0, 3.0], dtype="float16")})
apply_edits(df, {"edits": [{"rowIdx": 0, "columnId": "a", "value": "5.5"}]})
# before: dtype object, values ['5.5', 2.0, 3.0]   (corrupted)
# after:  dtype float16-compatible numeric, values [5.5, 2.0, 3.0]
```

marimo-team#10211 addressed `int8`/`uint8`/`float32` by switching to
`dtype.is_integer()` / `dtype.is_float()`, but `float16` still degrades:
narwhals (2.x) has no `Float16`, so it reports pandas `float16` as
`nw.Unknown`. Both predicate checks return `False` and `_convert_value`
falls through to `return str(value)`. The dtype-preservation test the PR
added for `float16` is currently **red on `main`**:

```
FAILED tests/_plugins/ui/_impl/test_data_editor.py::test_apply_edits_dataframe_small_width_numeric_preserves_dtype[float16]
```

## Fix

For an unsupported/`Unknown` dtype, fall through to the existing
original-value coercion (`_convert_value` already handles
`int`/`float`/`str`/…) instead of stringifying. For a numeric column the
original value is a Python `float`/`int`, so the edit stays numeric.
This is a superset fix — any dtype narwhals can't classify but whose
values are numeric now round-trips correctly.

## Tests

- The previously-failing
`test_apply_edits_dataframe_small_width_numeric_preserves_dtype[float16]`
now passes.
- Added `test_convert_value_unknown_dtype_coerces_from_original`, which
pins the fall-through directly against `nw.Unknown` (independent of
narwhals' evolving float16 mapping).
- `pytest tests/_plugins/ui/_impl/test_data_editor.py` → 107 passed;
`ruff check`/`ruff format` clean.

Follow-up to marimo-team#10211.

This branch was successfully deployed

1 active deployment
Preview — faa2bbd8 Deployed Jul 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants