繁体   English   中英

pandas read_json dtype=pd.CategoricalDtype 不起作用,但 dtype='category' 起作用

[英]pandas read_json dtype=pd.CategoricalDtype does not work but dtype='category' does

这是在 read_json 中指定CategoricalDtype dtype 不会转换列 dtype 的已知问题,还是代码中存在错误?

import pandas as pd

df = pd.read_json(
    "./data/data.json",
    dtype={
        #"facility": pd.CategoricalDtype, # does not work
        "facility": 'category',           # does work
        "supplier": pd.CategoricalDtype,  # does not work
    }
)
df.info()
-----
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   facility      232 non-null    category      
 3   supplier      111 non-null    object     

环境

MacOS 13.0.1 (22A400)
$ python --version
Python 3.9.13
$ pip list | grep pandas
pandas                      1.5.2

根据文档

由于 dtype='category' 本质上是 CategoricalDtype(None, False),并且由于所有 CategoricalDtype 实例比较等于“category”,所以 CategoricalDtype 的所有实例比较等于 CategoricalDtype(None, False),无论类别或顺序如何。

尝试:

"supplier": pd.CategoricalDtype()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM