Skip to content

Commit 1e75718

Browse files
committed
Fix dtypes for read_json
1 parent 8924277 commit 1e75718

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

pandas/io/json/_json.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,8 @@ def check_keys_split(self, decoded):
875875

876876
def parse(self):
877877

878-
# try numpy
879-
numpy = self.numpy
880-
if numpy:
878+
if self.numpy:
881879
self._parse_numpy()
882-
883880
else:
884881
self._parse_no_numpy()
885882

@@ -940,10 +937,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
940937
)
941938
if dtype is not None:
942939
try:
943-
# error: Argument 1 to "dtype" has incompatible type
944-
# "Union[ExtensionDtype, str, dtype[Any], Type[object]]";
945-
# expected "Type[Any]"
946-
dtype = np.dtype(dtype) # type: ignore[arg-type]
947940
return data.astype(dtype), True
948941
except (TypeError, ValueError):
949942
return data, False

pandas/tests/io/json/test_pandas.py

+28
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,34 @@ def test_from_json_to_json_table_dtypes(self):
13891389
result = read_json(dfjson, orient="table")
13901390
tm.assert_frame_equal(result, expected)
13911391

1392+
def test_to_json_from_json_columns_dtypes(self):
1393+
expected = DataFrame.from_dict(
1394+
{
1395+
"Integer": pd.Series([1, 2, 3], dtype="int64"),
1396+
"Float": pd.Series([None, 2.0, 3.0], dtype="float64"),
1397+
"Object": pd.Series([None, "", "c"], dtype="object"),
1398+
"Bool": pd.Series([True, False, True], dtype="bool"),
1399+
"Category": pd.Series(["a", "b", None], dtype="category"),
1400+
"Datetime": pd.Series(
1401+
["2020-01-01", None, "2020-01-03"], dtype="datetime64[ns]"
1402+
),
1403+
}
1404+
)
1405+
dfjson = expected.to_json(orient="columns")
1406+
result = read_json(
1407+
dfjson,
1408+
orient="columns",
1409+
dtype={
1410+
"Integer": "int64",
1411+
"Float": "float64",
1412+
"Object": "object",
1413+
"Bool": "bool",
1414+
"Category": "category",
1415+
"Datetime": "datetime64[ns]",
1416+
},
1417+
)
1418+
tm.assert_frame_equal(result, expected)
1419+
13921420
@pytest.mark.parametrize("dtype", [True, {"b": int, "c": int}])
13931421
def test_read_json_table_dtype_raises(self, dtype):
13941422
# GH21345

0 commit comments

Comments
 (0)