Skip to content

Commit

Permalink
replaced the test function for to_json with orient=table with differe…
Browse files Browse the repository at this point in the history
…nt combinations of non string column names
  • Loading branch information
chandra-teajunkie committed Feb 17, 2025
1 parent 11e2363 commit 96e8b59
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 96e8b59

Please sign in to comment.