From 5b12220e0f60ab8c18862a4d2789319c8301ca82 Mon Sep 17 00:00:00 2001 From: Pieterjan Robbe Date: Fri, 20 Nov 2026 15:47:56 -0800 Subject: [PATCH] fix(contribs): Table._clean() fails on float columns with NaN under pandas 3 fillna("") on a float64 column raises TypeError in pandas >= 3. Cast each column to object before replacing missing values; output is unchanged (missing values and +/-inf become "", all cells become strings). --- mp_api/client/contribs/_types.py | 6 ++++-- tests/client/contribs/test_types.py | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mp_api/client/contribs/_types.py b/mp_api/client/contribs/_types.py index 458ca689..9954f3b0 100644 --- a/mp_api/client/contribs/_types.py +++ b/mp_api/client/contribs/_types.py @@ -126,10 +126,12 @@ def from_dict( # type: ignore[override] def _clean(self): """Clean the dataframe.""" self.replace([np.inf, -np.inf], np.nan, inplace=True) - self.fillna("", inplace=True) self.index = self.index.astype(str) for col in self.columns: - self[col] = self[col].astype(str) + # cast to object before filling: pandas >= 3 refuses to put "" + # into a float column + values = self[col].astype(object) + self[col] = values.where(values.notna(), "").astype(str) def _attrs_as_dict(self): name = self.attrs.get("name", "table") diff --git a/tests/client/contribs/test_types.py b/tests/client/contribs/test_types.py index 5df86374..e0eadb14 100644 --- a/tests/client/contribs/test_types.py +++ b/tests/client/contribs/test_types.py @@ -85,3 +85,10 @@ def test_attachment(): # Finally test roundtrip assert json.loads(new_attach.unpack()) == data_struct + + +def test_table_clean_missing_values(): + table = Table({"x": [1.0, float("nan"), float("inf")], "y": [1, 2, 3]}) + table._clean() + assert table["x"].tolist() == ["1.0", "", ""] + assert table["y"].tolist() == ["1", "2", "3"]