Skip to content

Commit 96e8b59

Browse files
replaced the test function for to_json with orient=table with different combinations of non string column names
1 parent 11e2363 commit 96e8b59

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

pandas/tests/io/json/test_pandas.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,27 @@ def test_large_number():
22852285
tm.assert_series_equal(result, expected)
22862286

22872287

2288-
def test_to_json_table_numeric_column_names():
2289-
df = DataFrame([[1, 2], [3, 4]], columns=[1, 2])
2290-
with pytest.raises(ValueError, match="Column names must be strings"):
2291-
df.to_json(orient="table")
2288+
@pytest.mark.parametrize(
2289+
"df, should_fail",
2290+
[
2291+
(DataFrame([[1, 2], [3, 4]], columns=[1, 2]), True),
2292+
(DataFrame([[1, 2], [3, 4]], columns=[1.1, 2.2]), True),
2293+
(DataFrame([[1, 2], [3, 4]], columns=[None, "valid"]), True),
2294+
(DataFrame([[1, 2], [3, 4]], columns=[True, False]), True),
2295+
(DataFrame([[1, 2], [4, 5]], columns=["a", "b"]), False),
2296+
],
2297+
)
2298+
def test_to_json_table_non_string_column_names(df, should_fail):
2299+
if should_fail:
2300+
with pytest.raises(ValueError, match="Column names must be strings"):
2301+
df.to_json(orient="table")
2302+
else:
2303+
expected = {
2304+
"schema": pd.io.json.build_table_schema(df, index=False),
2305+
"data": df.to_dict(orient="records"),
2306+
}
2307+
2308+
result = df.to_json(orient="table", index=False)
2309+
result = json.loads(result)
2310+
2311+
assert result == expected

0 commit comments

Comments
 (0)