Skip to content

BUG: int types are converted to float when using to_dict() and iloc functions together #50633

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
2 of 3 tasks
DanielHabenicht opened this issue Jan 8, 2023 · 1 comment
Closed
2 of 3 tasks
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question

Comments

@DanielHabenicht
Copy link

DanielHabenicht commented Jan 8, 2023

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

>>> pd.DataFrame({"int":[1,2,3,4],"float":[1.0,1.0,1.0,1.0]}).iloc[0].to_dict()
{'int': 1.0, 'float': 1.0}

Issue Description

When using the to_dict() function the dtype is converted to float although it should still be int

Expected Behavior

>>> pd.DataFrame({"int":[1,2,3,4],"float":[1.0,1.0,1.0,1.0]}).iloc[0].to_dict()
{'int': 1, 'float': 1.0}

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.10.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.79.1-microsoft-standard-WSL2
Version : #1 SMP Wed Nov 23 01:01:46 UTC 2022
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.7.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Might be related:
#21256
#20791

@DanielHabenicht DanielHabenicht added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2023
@phofl
Copy link
Member

phofl commented Jan 8, 2023

Hi, thanks for your report. This is expected and unrelated to to_dict. The iloc call returns a Series which tries to find a common dtype, e.g. float. Hence the conversion happens

@phofl phofl closed this as completed Jan 8, 2023
@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question
Projects
None yet
Development

No branches or pull requests

2 participants