diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 361e45ddc16c5..aff910073afeb 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -2285,7 +2285,27 @@ def test_large_number(): tm.assert_series_equal(result, expected) -def test_to_json_table_numeric_column_names(): - df = DataFrame([[1, 2], [3, 4]], columns=[1, 2]) - with pytest.raises(ValueError, match="Column names must be strings"): - df.to_json(orient="table") +@pytest.mark.parametrize( + "df, should_fail", + [ + (DataFrame([[1, 2], [3, 4]], columns=[1, 2]), True), + (DataFrame([[1, 2], [3, 4]], columns=[1.1, 2.2]), True), + (DataFrame([[1, 2], [3, 4]], columns=[None, "valid"]), True), + (DataFrame([[1, 2], [3, 4]], columns=[True, False]), True), + (DataFrame([[1, 2], [4, 5]], columns=["a", "b"]), False), + ], +) +def test_to_json_table_non_string_column_names(df, should_fail): + if should_fail: + with pytest.raises(ValueError, match="Column names must be strings"): + df.to_json(orient="table") + else: + expected = { + "schema": pd.io.json.build_table_schema(df, index=False), + "data": df.to_dict(orient="records"), + } + + result = df.to_json(orient="table", index=False) + result = json.loads(result) + + assert result == expected