Skip to content

Commit 754a2ed

Browse files
pandas 3.0 fixes
1 parent c22e255 commit 754a2ed

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

mpcontribs-client/mpcontribs/client/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,16 @@ def from_dict(cls, dct: dict):
398398
"""
399399
df = pd.DataFrame.from_records(
400400
dct["data"], columns=dct["columns"], index=dct["index"]
401-
).apply(pd.to_numeric, errors="ignore")
402-
df.index = pd.to_numeric(df.index, errors="ignore")
401+
)
402+
for col in df.columns:
403+
try:
404+
df[col] = df[col].apply(pd.to_numeric)
405+
except Exception as exc:
406+
continue
407+
try:
408+
df.index = pd.to_numeric(df.index)
409+
except Exception:
410+
pass
403411
labels = dct["attrs"].get("labels", {})
404412

405413
if "index" in labels:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from mpcontribs.client import Table
2+
3+
4+
def test_table():
5+
6+
data = {
7+
"first_name": ["Todd", "Willie", "Mike"],
8+
"family_name": ["Bonzalez", "Dustice", "Truk"],
9+
"age": [31, 24, 28],
10+
"batting_average": [0.777, 0.5, 0.81],
11+
}
12+
test_table = Table(data)
13+
14+
# Calling `as_dict` transforms the data in a `Table`
15+
table_as_dict = Table(test_table.copy()).as_dict()
16+
assert all(
17+
table_as_dict.get(k) for k in ("attrs", "columns", "data", "index", "name")
18+
)
19+
table_info = test_table.info()
20+
assert {y.strip() for y in table_info["columns"].split(",")} == set(
21+
test_table.columns
22+
)
23+
assert table_info["nrows"] == len(test_table)
24+
25+
table_roundtrip = Table.from_dict(table_as_dict)
26+
27+
# `tolist()` needed to compare base python types
28+
for t in (test_table, table_roundtrip):
29+
assert all(
30+
isinstance(v, str)
31+
for col in ("family_name", "first_name")
32+
for v in t[col].tolist()
33+
)
34+
assert all(isinstance(v, int) for v in t.age.tolist())
35+
assert all(isinstance(v, float) for v in t.batting_average.tolist())

0 commit comments

Comments
 (0)