Skip to content

BUG: read_json raising with table orient and NA values #50332

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

Merged
merged 2 commits into from
Dec 19, 2022
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ I/O
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
- Bug in :func:`read_sql_query` ignoring ``dtype`` argument when ``chunksize`` is specified and result is empty (:issue:`50245`)
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
- Bug in :func:`read_json` raising with ``orient="table"`` and ``NA`` value (:issue:`40255`)
- Bug in displaying ``string`` dtypes not showing storage option (:issue:`50099`)
- Bug in :func:`DataFrame.to_string` with ``header=False`` that printed the index name on the same line as the first row of the data (:issue:`49230`)
- Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`)
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/json/_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ def convert_json_field_to_pandas_type(field) -> str | CategoricalDtype:
if typ == "string":
return "object"
elif typ == "integer":
return "int64"
return field.get("extDtype", "int64")
elif typ == "number":
return "float64"
return field.get("extDtype", "float64")
elif typ == "boolean":
return "bool"
return field.get("extDtype", "bool")
elif typ == "duration":
return "timedelta64"
elif typ == "datetime":
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/io/json/test_json_table_schema_ext_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import pytest

from pandas import (
NA,
DataFrame,
Index,
array,
read_json,
)
import pandas._testing as tm
from pandas.core.arrays.integer import Int64Dtype
from pandas.core.arrays.string_ import StringDtype
from pandas.core.series import Series
Expand Down Expand Up @@ -273,3 +277,43 @@ def test_to_json(self, df):
expected = OrderedDict([("schema", schema), ("data", data)])

assert result == expected

def test_json_ext_dtype_reading_roundtrip(self):
# GH#40255
df = DataFrame(
{
"a": Series([2, NA], dtype="Int64"),
"b": Series([1.5, NA], dtype="Float64"),
"c": Series([True, NA], dtype="boolean"),
},
index=Index([1, NA], dtype="Int64"),
)
expected = df.copy()
data_json = df.to_json(orient="table", indent=4)
result = read_json(data_json, orient="table")
tm.assert_frame_equal(result, expected)

def test_json_ext_dtype_reading(self):
# GH#40255
data_json = """{
"schema":{
"fields":[
{
"name":"a",
"type":"integer",
"extDtype":"Int64"
}
],
},
"data":[
{
"a":2
},
{
"a":null
}
]
}"""
result = read_json(data_json, orient="table")
expected = DataFrame({"a": Series([2, NA], dtype="Int64")})
tm.assert_frame_equal(result, expected)