Skip to content

Fix read_json category dtype #30728

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

Closed
Closed
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
10 changes: 8 additions & 2 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
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 (
pandas_dtype,
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 +897,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 = pandas_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 @@ -8,6 +8,7 @@
import pytest

from pandas.compat import is_platform_32bit, is_platform_windows
from pandas.core.dtypes.common import is_categorical
import pandas.util._test_decorators as td

import pandas as pd
Expand Down Expand Up @@ -1197,6 +1198,17 @@ 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": "A"}\n'
'{"a": 3, "b": "B"}\n'
)
json = StringIO(json)
result = read_json(json, lines=True, dtype={"b": "category"})
assert is_categorical(result["b"])

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