Skip to content

Commit 8de37e9

Browse files
committed
Fix read_json category dtype
1 parent 2a3d840 commit 8de37e9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pandas/io/json/_json.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pandas._typing import JSONSerializable
1313
from pandas.errors import AbstractMethodError
1414

15-
from pandas.core.dtypes.common import ensure_str, is_period_dtype
15+
from pandas.core.dtypes.common import ensure_str, is_period_dtype, is_categorical_dtype
1616

1717
from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
1818
from pandas.core.construction import create_series_with_explicit_dtype
@@ -892,7 +892,8 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
892892
)
893893
if dtype is not None:
894894
try:
895-
dtype = np.dtype(dtype)
895+
if not is_categorical_dtype(dtype):
896+
dtype = np.dtype(dtype)
896897
return data.astype(dtype), True
897898
except (TypeError, ValueError):
898899
return data, False

pandas/tests/io/json/test_pandas.py

+12
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,18 @@ def test_read_local_jsonl(self):
11971197
expected = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
11981198
tm.assert_frame_equal(result, expected)
11991199

1200+
def test_read_json_category_dtype(self):
1201+
json = (
1202+
'{"a": 0, "b": "A"}\n'
1203+
'{"a": 1, "b": "B"}\n'
1204+
'{"a": 2, "b": "B"}\n'
1205+
'{"a": 3, "b": "B"}\n'
1206+
)
1207+
json = StringIO(json)
1208+
result = read_json(json, lines=True, dtype={"a": "category"})
1209+
expected = DataFrame([[0, "foo"], [1, "bar"], [2, "foo"], [3, "bar"]])
1210+
tm.assert_frame_equal(result, expected)
1211+
12001212
def test_read_jsonl_unicode_chars(self):
12011213
# GH15132: non-ascii unicode characters
12021214
# \u201d == RIGHT DOUBLE QUOTATION MARK

0 commit comments

Comments
 (0)