Skip to content

BUG: cannot use category dtype in pandas read_json method  #40063

@alison199

Description

@alison199
  • I have confirmed this bug exists on the latest version of pandas.

import pandas as pd

print(pd.version)

1.2.2

Problem described in other places:

  1. pd.read_json ignores 'category' dtypes #21892
  2. BUG: Using categorical dtype in read_json #25573
  3. Fix read_json category dtype #30728

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

content = '[{"score": 1,"color":"red"},{"score":10,"color":"green"}]'
dtypes_dict = {'score': 'uint8', 'colour': 'category'}

data_frame = pd.read_json(content, dtype=dtypes_dict)
print(data_frame.dtypes)

Problem description

When I run the above, I get the following dtypes for the dataframe columns:

score uint8
color object

Expected Output

score uint8
color category

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeDuplicate ReportDuplicate issue or pull requestIO JSONread_json, to_json, json_normalize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions