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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: graceful fallback for unsupported extension dtypes in to_arrow_ipc (#8785)
Andrew Barnes615482df24ed
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 1ea4575d45..fc0449b4cc 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -238,7 +238,24 @@ class PandasTableManagerFactory(TableManagerFactory):def to_arrow_ipc(self) -> bytes:out = io.BytesIO()- self._original_data.to_feather(out, compression="uncompressed")+ try:+ self._original_data.to_feather(+ out, compression="uncompressed"+ )+ except Exception:+ # Fall back: convert extension-type columns that+ # PyArrow cannot handle (e.g. pint-pandas) to plain+ # values so the feather write can succeed.+ import pyarrow as pa++ out = io.BytesIO()+ df = self._original_data.copy()+ for col in df.columns:+ try:+ pa.Array.from_pandas(df[col])+ except Exception:+ df[col] = df[col].astype(object).infer_objects()+ df.to_feather(out, compression="uncompressed")return out.getvalue()def apply_formatting(diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex 8e09d3fc37..e08626aae2 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -2075,3 +2075,39 @@ class TestPandasTableManager(unittest.TestCase):assert result.get_num_rows() == 1# MultiIndex should be preserved with original namesassert list(result._original_data.index.names) == ["x", "level"]++ def test_to_arrow_ipc_fallback_for_unsupported_extension_dtype(+ self,+ ) -> None:+ """to_arrow_ipc falls back when a column has an extension dtype+ that PyArrow cannot convert (e.g. pint-pandas)."""+ from unittest.mock import patch++ import pyarrow as pa++ df = pd.DataFrame({"a": [1, 2, 3], "b": [4.0, 5.0, 6.0]})+ manager = self.factory.create()(df)++ # Make the first to_feather call raise, simulating an+ # unsupported extension dtype.+ original_to_feather = pd.DataFrame.to_feather++ call_count = 0++ def patched_to_feather(self_df, *args: Any, **kwargs: Any):+ nonlocal call_count+ call_count += 1+ if call_count == 1:+ raise pa.lib.ArrowTypeError("unsupported extension type")+ return original_to_feather(self_df, *args, **kwargs)++ with patch.object(pd.DataFrame, "to_feather", patched_to_feather):+ result = manager.to_arrow_ipc()++ assert isinstance(result, bytes)+ assert len(result) > 0+ # Verify the result is valid IPC/feather data by reading it back+ buf = pa.BufferReader(result)+ table = pa.ipc.open_file(buf).read_all()+ assert table.num_rows == 3+ assert set(table.column_names) == {"a", "b"}