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"]