Skip to content

BUG: incompatible dtype when creating string column with loc #55733

@5j9

Description

@5j9

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({"x": [0, 1, 2, 3]})

new_cols = pd.DataFrame(
    {"y": ["a", "b"], "z": ["c", "d"]}, index=[1, 2]
)

df.loc[slice(1, 3), ["y", "z"]] = new_cols

print(df)

Issue Description

Produces the following warnings:

....\~test2.py:9: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['a' 'b' nan]' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  df.loc[slice(1, 3), ["y", "z"]] = new_cols
....\~test2.py:9: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['c' 'd' nan]' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  df.loc[slice(1, 3), ["y", "z"]] = new_cols

Expected Behavior

No warning.

Installed Versions

INSTALLED VERSIONS

commit : b7ab856
python : 3.11.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 0+untagged.1.gb7ab856
numpy : 1.26.0
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : 7.4.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.16.1
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugIndexingRelated to indexing on series/frames, not to indexes themselvesWarningsWarnings that appear or should be added to pandas

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions