Skip to content

Commit 4f49f01

Browse files
committed
BUG: Raise ValueError for non-string columns in read_json orient='table' (GH19129)
1 parent d1c6404 commit 4f49f01

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pandas/io/json/_table_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ def parse_table_schema(json, precise_float: bool) -> DataFrame:
372372
pandas.read_json
373373
"""
374374
table = ujson_loads(json, precise_float=precise_float)
375+
fields = table["schema"]["fields"]
376+
377+
if any(not isinstance(field["name"], str) for field in fields):
378+
raise ValueError("All column names must be strings when using orient='table'.")
379+
375380
col_order = [field["name"] for field in table["schema"]["fields"]]
376381
df = DataFrame(table["data"], columns=col_order)[col_order]
377382

pandas/tests/io/json/test_json_table_schema.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,22 @@ def test_read_json_table_orient_period_depr_freq(self, freq):
881881
out = StringIO(df.to_json(orient="table"))
882882
result = pd.read_json(out, orient="table")
883883
tm.assert_frame_equal(df, result)
884+
885+
def test_read_json_table_non_string_column_names(self) -> None:
886+
bad_json = json.dumps({
887+
"schema": {
888+
"fields": [
889+
{"name": 0, "type": "integer"},
890+
{"name": 1, "type": "string"}
891+
],
892+
"primaryKey": [],
893+
"pandas_version": "1.0.0"
894+
},
895+
"data": [
896+
[1, "a"],
897+
[2, "b"]
898+
]
899+
})
900+
901+
with pytest.raises(ValueError, match="All column names must be strings when using orient='table'"):
902+
pd.read_json(StringIO(bad_json), orient="table")

0 commit comments

Comments
 (0)