Skip to content

BUG: df.to_dict('record') casts ints to floats #13817

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
lamyiowce opened this issue Jul 27, 2016 · 2 comments
Closed

BUG: df.to_dict('record') casts ints to floats #13817

lamyiowce opened this issue Jul 27, 2016 · 2 comments
Labels
Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request

Comments

@lamyiowce
Copy link

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({"a": [1, 2], "b": [42, 65], "c": [0.5, 0.4]})
df.to_dict("records")
>>> [{'a': 1.0, 'b': 42.0, 'c': 0.5},
     {'a': 2.0, 'b': 65.0, 'c': 0.40000000000000002}]

Expected Output

[{'a': 1, 'b': 42, 'c': 0.5},
 {'a': 2, 'b': 65, 'c': 0.40000000000000002}]

comment

Same bug for df.to_dict("index") and df.to_dict("split"), but not for the rest. If all data is integer, the function works properly.

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 3.16.0-38-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: pl_PL.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.1
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.4.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None
@TomAugspurger
Copy link
Contributor

TomAugspurger commented Jul 27, 2016

I thought we already had an issue for this, but I couldn't find it.

Looks to be somewhere around here: https://github.com/pydata/pandas/blob/31f8e4dc8af8f0d109f366d0b726aef210bf7904/pandas/core/frame.py#L897

the .values is casting everything to float. I think using self.itertuples would fix the problem. Would you mind checking that out, and submitting a pull request with a fix?

@TomAugspurger TomAugspurger added Difficulty Novice Dtype Conversions Unexpected or buggy dtype conversions labels Jul 27, 2016
@TomAugspurger
Copy link
Contributor

Found the other issue this is a dupe of: #12859

Closing this one, you can have a look over there. Looks like we're debating whether this would be an API change, there's also a workaround there for you.

@TomAugspurger TomAugspurger added the Duplicate Report Duplicate issue or pull request label Jul 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants