Skip to content

BUG: to_dict("index") and to_dict("list") don't coerce to native types #46751

Closed
@RogerThomas

Description

@RogerThomas

Pandas version checks

  • I have checked that this issue has not already been reported.

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd


def main():
    i64 = np.int64
    f64 = np.float64
    df = pd.DataFrame({
        "A": [i64(1), "ASD", f64(2.2)],
        "B": [(2,), f64(2.2), i64(1)],
    })
    a = df.to_dict("list")
    print("to_dict('list')")
    for key, list_ in a.items():
        for value in list_:
            print(f"{key}: {value}: {type(value)}")
    b = df.to_dict("index")
    print("to_dict('index')")
    for index, obj in b.items():
        for key, value in obj.items():
            print(f"{key}: {value}: {type(value)}")


if __name__ == "__main__":
    main()

Issue Description

When using to_dict ONLY for orient=list/index the values are not coerced to python native types. We are simply missing a maybe_box_native call

Expected Behavior

Each value in the resulting to_dict output should be Python native types, instead we are getting int64's and float64's.

This ONLY happens for orient=list and orient=index, the other orients correctly coerce to native types.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 4bfe3d0 python : 3.9.10.final.0 python-bits : 64 OS : Darwin OS-release : 20.6.0 Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8

pandas : 1.4.2
numpy : 1.22.1
pytz : 2021.1
dateutil : 2.8.1
pip : 22.0.3
setuptools : 57.0.0
Cython : 0.29.23
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.1
jinja2 : 3.0.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : 0.6.2
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.4.2
numba : 0.53.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.6.3
snappy : None
sqlalchemy : 1.1.12
tables : None
tabulate : None
xarray : None
xlrd : 1.1.0
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorBugDtype ConversionsUnexpected or buggy dtype conversionsIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions