Skip to content

BUG FIX: to_json() with JSON Table Schema work correctly with string dtype. #61900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions pandas/io/json/_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ def as_json_table_type(x: DtypeObj) -> str:
return "datetime"
elif lib.is_np_dtype(x, "m"):
return "duration"
elif isinstance(x, ExtensionDtype):
return "any"
elif is_string_dtype(x):
return "string"
else:
Expand Down Expand Up @@ -197,7 +195,7 @@ def convert_json_field_to_pandas_type(field) -> str | CategoricalDtype:
"""
typ = field["type"]
if typ == "string":
return "object"
return field.get("extDtype", None)
elif typ == "integer":
return field.get("extDtype", "int64")
elif typ == "number":
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/io/json/test_json_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_build_table_schema(self, df_schema, using_infer_string):
"primaryKey": ["idx"],
}
if using_infer_string:
expected["fields"][2] = {"name": "B", "type": "any", "extDtype": "str"}
expected["fields"][2] = {"name": "B", "type": "string", "extDtype": "str"}
assert result == expected
result = build_table_schema(df_schema)
assert "pandas_version" in result
Expand Down Expand Up @@ -120,10 +120,10 @@ def test_multiindex(self, df_schema, using_infer_string):
if using_infer_string:
expected["fields"][0] = {
"name": "level_0",
"type": "any",
"type": "string",
"extDtype": "str",
}
expected["fields"][3] = {"name": "B", "type": "any", "extDtype": "str"}
expected["fields"][3] = {"name": "B", "type": "string", "extDtype": "str"}
assert result == expected

df.index.names = ["idx0", None]
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_to_json(self, df_table, using_infer_string):
]

if using_infer_string:
fields[2] = {"name": "B", "type": "any", "extDtype": "str"}
fields[2] = {"name": "B", "type": "string", "extDtype": "str"}

schema = {"fields": fields, "primaryKey": ["idx"]}
data = [
Expand Down Expand Up @@ -547,7 +547,7 @@ def test_convert_pandas_type_to_json_field_categorical(self, kind, ordered):
},
CategoricalDtype(categories=["a", "b", "c"], ordered=True),
),
({"type": "string"}, "object"),
({"type": "string"}, None),
],
)
def test_convert_json_field_to_pandas_type(self, inp, exp):
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/io/json/test_json_table_schema_ext_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_build_table_schema(self):
{"name": "index", "type": "integer"},
{"name": "A", "type": "any", "extDtype": "DateDtype"},
{"name": "B", "type": "number", "extDtype": "decimal"},
{"name": "C", "type": "any", "extDtype": "string"},
{"name": "C", "type": "string", "extDtype": "string"},
{"name": "D", "type": "integer", "extDtype": "Int64"},
],
"primaryKey": ["index"],
Expand Down Expand Up @@ -80,10 +80,10 @@ def test_as_json_table_type_ext_decimal_dtype(self):
@pytest.mark.parametrize("box", [lambda x: x, Series])
def test_as_json_table_type_ext_string_array_dtype(self, box):
string_data = box(array(["pandas"], dtype="string"))
assert as_json_table_type(string_data.dtype) == "any"
assert as_json_table_type(string_data.dtype) == "string"

def test_as_json_table_type_ext_string_dtype(self):
assert as_json_table_type(StringDtype()) == "any"
assert as_json_table_type(StringDtype()) == "string"

@pytest.mark.parametrize("box", [lambda x: x, Series])
def test_as_json_table_type_ext_integer_array_dtype(self, box):
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_build_string_series(self, sa):

fields = [
{"name": "id", "type": "integer"},
{"name": "a", "type": "any", "extDtype": "string"},
{"name": "a", "type": "string", "extDtype": "string"},
]

schema = {"fields": fields, "primaryKey": ["id"]}
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_to_json(self, da, dc, sa, ia):
OrderedDict({"name": "idx", "type": "integer"}),
OrderedDict({"name": "A", "type": "any", "extDtype": "DateDtype"}),
OrderedDict({"name": "B", "type": "number", "extDtype": "decimal"}),
OrderedDict({"name": "C", "type": "any", "extDtype": "string"}),
OrderedDict({"name": "C", "type": "string", "extDtype": "string"}),
OrderedDict({"name": "D", "type": "integer", "extDtype": "Int64"}),
]

Expand Down
Loading