Skip to content
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

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

Closed
2 of 3 tasks
mwouts opened this issue Feb 3, 2024 · 2 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@mwouts
Copy link

mwouts commented Feb 3, 2024

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

@mwouts mwouts added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 3, 2024
mwouts added a commit to mwouts/itables that referenced this issue Feb 3, 2024
* Fix complex table footers

* Address pytest.skip warning
pytest.PytestRemovedIn8Warning: pytest.skip(msg=...) is now deprecated, use pytest.skip(reason=...) instead

* Address pandas warning
FutureWarning: 'S' is deprecated and will be removed in a future version, please use 's' instead.

* Remove environment2.yml

* Adjust the tests for pandas=2.2.0
pandas-dev/pandas#57229
pandas-dev/pandas#55080
@phofl
Copy link
Member

phofl commented Feb 3, 2024

You should use isetitem if you don't want to modify your object inplace and thus avoid the warning

df.isetitem(i, df.iloc[:,i].astype(str))

@phofl phofl closed this as completed Feb 3, 2024
@mwouts
Copy link
Author

mwouts commented Feb 4, 2024

Sounds great! Thank you for pointing out at isetitem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants