Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pandas._typing import JSONSerializable
from pandas.errors import AbstractMethodError

from pandas.core.dtypes.common import ensure_str, is_period_dtype
from pandas.core.dtypes.common import ensure_str, is_period_dtype, is_categorical_dtype

from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
from pandas.core.construction import create_series_with_explicit_dtype
Expand Down Expand Up @@ -892,7 +892,8 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
)
if dtype is not None:
try:
dtype = np.dtype(dtype)
if not is_categorical_dtype(dtype):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use pandas_dtype here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need both the is_categorical_dtype and pandas_dtype here; just the latter should be OK

dtype = np.dtype(dtype)
return data.astype(dtype), True
except (TypeError, ValueError):
return data, False
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,18 @@ def test_read_local_jsonl(self):
expected = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
tm.assert_frame_equal(result, expected)

def test_read_json_category_dtype(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will need to test all extension dtypes that we support, pls parametrize this over interval, period, categorical, datetime w/tz

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, according to you, what's the best way to deal/pass different input dfs and expected dfs ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taoufik07 have a go using https://docs.pytest.org/en/latest/parametrize.html, there's lots of examples of it being used it other parts of the test suite

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know about parametrize, but I find having multiple lines (type, input, expected) unpleasant to the eye and I tought that there was some magic function that handles the data generations for all these types, maybe I will just go with the naive way

json = (
'{"a": 0, "b": "A"}\n'
'{"a": 1, "b": "B"}\n'
'{"a": 2, "b": "B"}\n'
'{"a": 3, "b": "B"}\n'
)
json = StringIO(json)
result = read_json(json, lines=True, dtype={"a": "category"})
expected = DataFrame([[0, "foo"], [1, "bar"], [2, "foo"], [3, "bar"]])
tm.assert_frame_equal(result, expected)

def test_read_jsonl_unicode_chars(self):
# GH15132: non-ascii unicode characters
# \u201d == RIGHT DOUBLE QUOTATION MARK
Expand Down