Skip to content

BUG: A Warning is emitted in Pandas 2.2.0 when I change the type of a column using df.iloc[:, i]=... #57229

@mwouts

Description

@mwouts

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

df = pd.DataFrame({'a': [1]})

# Change the type of colum 0.
# NB I Use column indices as sometimes 
# df has duplicated column names
i = 0
x = df.iloc[:, i]
df.iloc[:,i] = x.astype(str)

Issue Description

When I replace an integer column by its string representation, the following warning is emitted (see mwouts/itables#223):

FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '0    1
Name: a, dtype: object' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  df.iloc[:,i] = x.astype(str)

Expected Behavior

I think the warning makes sense when I replace only a fraction of the rows, but here I replace the full column, so I am not sure the warning is legit. Do you think we could drop the warning when the full column is replaced, or is there a better way to replace the i-th column?

Thanks!

Installed Versions

pd.show_versions() INSTALLED VERSIONS ------------------ commit : fd3f57170aa1af588ba877e8e28c158a20a4886d python : 3.11.7.final.0 python-bits : 64 OS : Linux OS-release : 6.5.0-14-generic Version : #14~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Nov 20 18:15:30 UTC 2 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.20.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.12.2
gcsfs : 2023.12.2post1
matplotlib : 3.8.2
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.25
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.4.1
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