Skip to content

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

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
3 tasks done
RogerThomas opened this issue Apr 12, 2022 · 4 comments · Fixed by #46752
Closed
3 tasks done

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

RogerThomas opened this issue Apr 12, 2022 · 4 comments · Fixed by #46752
Labels
API - Consistency Internal Consistency of API/Behavior Bug Dtype Conversions Unexpected or buggy dtype conversions IO Data IO issues that don't fit into a more specific label
Milestone

Comments

@RogerThomas
Copy link
Contributor

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

@RogerThomas RogerThomas added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2022
@simonjayhawkins
Copy link
Member

Thanks @RogerThomas for the report.

note there is an open issue #25969 related to this and also Series.items().

I will label as a duplicate but keep this one open for now since it is not immediately apparent what is outstanding to close #25969 and this issue is at least explicit on the orient argument which is not mentioned in #25969

@simonjayhawkins simonjayhawkins added Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request API - Consistency Internal Consistency of API/Behavior and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2022
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Apr 12, 2022
@RogerThomas
Copy link
Contributor Author

RogerThomas commented Apr 12, 2022

Thanks @simonjayhawkins, sorry I didn't see that issue. I would imagine my PR would fix that issue

@simonjayhawkins
Copy link
Member

There maybe other issues (excluding the EA ones) related to this that it maybe worth checking if your PR fixes https://github.com/pandas-dev/pandas/search?q=to_dict+native+python&state=open&type=issues

@RogerThomas
Copy link
Contributor Author

@simonjayhawkins ok so it looks like;

29824 has already been fixed

There is no change in 25969, it looks like it has been semi-fixed already but there is still an issue in it.

21256 has alread been fixed apart from the Timestamp

Does not fix 37642

34665 has already been fixed

@rhshadrach rhshadrach added IO Data IO issues that don't fit into a more specific label and removed Duplicate Report Duplicate issue or pull request labels Apr 19, 2022
@rhshadrach rhshadrach modified the milestones: Contributions Welcome, 1.5 Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Dtype Conversions Unexpected or buggy dtype conversions IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants