Skip to content

BUG: use of iloc with heterogeneous DataFrame sometimes performs undocumented conversions #61537

Open
@illbebach

Description

@illbebach

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 pandas as pd

d = {
    'Name': ['Bob', 'John', 'Alice'],
    'Age': [25, 41, 30],
    'Result': [1.2, 0.5, 0.3],
    'Ok': [True, False, True],
}
df = pd.DataFrame(data=d)

print()
print('------ test 1 ------')
print(df)
print()
print('first row')
print(df.iloc[0])


d = {
    'Age': [25, 41, 30],
    'Result': [1.2, 0.5, 0.3],
    'Ok': [True, False, True],
}
df = pd.DataFrame(data=d)

print()
print('------ test 2 ------')
print(df)
print()
print('first row')
print(df.iloc[0])


d = {
    'Age': [25, 41, 30],
    'Result': [1.2, 0.5, 0.3],
}
df = pd.DataFrame(data=d)

print()
print('------ test 3 ------')
print(df)
print()
print('first row')
print(df.iloc[0])

Issue Description

Pandas sometimes performs type conversions on returned data from the .iloc function.

In the first two cases, a Series of object is returned. In the last case pandas decided to promote all values to float, and returns a Series of float, which is not what I want.

Program output


------ test 1 ------
    Name  Age  Result     Ok
0    Bob   25     1.2   True
1   John   41     0.5  False
2  Alice   30     0.3   True

first row
Name       Bob
Age         25
Result     1.2
Ok        True
Name: 0, dtype: object

------ test 2 ------
   Age  Result     Ok
0   25     1.2   True
1   41     0.5  False
2   30     0.3   True

first row
Age         25
Result     1.2
Ok        True
Name: 0, dtype: object

------ test 3 ------
   Age  Result
0   25     1.2
1   41     0.5
2   30     0.3

first row
Age       25.0
Result     1.2
Name: 0, dtype: float64
  1. To me, it is undesirable, in any circumstance, for pandas to apply type conversions on returned data. I realize this conversion is probably there for historical or performance reasons, and may not be changed.
  2. At a minimum, the documentation should mention exactly what circumstances a type conversion will occur. The .iloc documentation makes no mention of what will occur when a DataFrame containing heterogeneous data is indexed via .iloc

For completeness, there is a similar bug logged long ago. #5256

Expected Behavior

  1. My preference is to never perform type conversions. I realize changing this behavior could break some existing code that depends on such a conversion.
  2. My second recommendation is to update the documentation to describe exactly when and what data conversions will be performed by pandas. At a minimum there should be a 'warning' or 'note' about the type conversions.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.10
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.3
numpy : 2.2.6
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.1.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.10.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions