Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions mp_api/client/contribs/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions tests/client/contribs/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]