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
12 changes: 10 additions & 2 deletions mpcontribs-client/mpcontribs/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,16 @@ def from_dict(cls, dct: dict):
"""
df = pd.DataFrame.from_records(
dct["data"], columns=dct["columns"], index=dct["index"]
).apply(pd.to_numeric, errors="ignore")
df.index = pd.to_numeric(df.index, errors="ignore")
)
for col in df.columns:
try:
df[col] = df[col].apply(pd.to_numeric)
except Exception:
continue
try:
df.index = pd.to_numeric(df.index)
except Exception:
pass
labels = dct["attrs"].get("labels", {})

if "index" in labels:
Expand Down
35 changes: 35 additions & 0 deletions mpcontribs-client/tests/test_data_structs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from mpcontribs.client import Table


def test_table():

data = {
"first_name": ["Todd", "Willie", "Mike"],
"family_name": ["Bonzalez", "Dustice", "Truk"],
"age": [31, 24, 28],
"batting_average": [0.777, 0.5, 0.81],
}
test_table = Table(data)

# Calling `as_dict` transforms the data in a `Table`
table_as_dict = Table(test_table.copy()).as_dict()
assert all(
table_as_dict.get(k) for k in ("attrs", "columns", "data", "index", "name")
)
table_info = test_table.info()
assert {y.strip() for y in table_info["columns"].split(",")} == set(
test_table.columns
)
assert table_info["nrows"] == len(test_table)

table_roundtrip = Table.from_dict(table_as_dict)

# `tolist()` needed to compare base python types
for t in (test_table, table_roundtrip):
assert all(
isinstance(v, str)
for col in ("family_name", "first_name")
for v in t[col].tolist()
)
assert all(isinstance(v, int) for v in t.age.tolist())
assert all(isinstance(v, float) for v in t.batting_average.tolist())