I have a json file that looks like this:
[{"A": 0, "B": "x"}, {"A": 1, "B": "y", "C": 0}, {"A": 2, "B": "z", "C": 1}]
Since the "C" column contains a NaN value (first row), pandas automatically infers that its dtype is "float64":
>>> pd.read_json(path).C.dtype
dtype('float64')
However, I want the dtype of the "C" column to be "Int32". pd.read_json(path, dtype={"C": "Int32"}) does not work:
>>> pd.read_json(path, dtype={"C": "Int32"}).C.dtype
dtype('float64')
Instead, pd.read_json(path).astype({"C": "Int32"}) does work:
>>> pd.read_json(path).astype({"C": "Int32"}).C.dtype
Int32Dtype()
Why does this happen? How can I set the correct dtype by only using the pd.read_json function?