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: incompatible dtype when creating string column with loc #55733

Closed
3 tasks done
5j9 opened this issue Oct 27, 2023 · 4 comments
Closed
3 tasks done

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

5j9 opened this issue Oct 27, 2023 · 4 comments
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas
Milestone

Comments

@5j9
Copy link
Contributor

5j9 commented Oct 27, 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

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

@5j9 5j9 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 27, 2023
@mroeschke
Copy link
Member

cc @MarcoGorelli looks like assignment that expands (filling with NaN) should ignore the PDEP 6 warning?

@mroeschke mroeschke added Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 27, 2023
@mroeschke mroeschke added this to the 2.1.3 milestone Oct 27, 2023
@MarcoGorelli MarcoGorelli self-assigned this Oct 31, 2023
@hvsesha
Copy link

hvsesha commented Nov 1, 2023

@mroeschke
Instead of starting the slice position from 1 if we start from zero we are not getting the warnings

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

Kindly advise what other way need to fix the issue .

If you try slice with 0,3 it should work

@MarcoGorelli
Copy link
Member

The issue is that, before setting the new values, a new float block is being created

blocks.append(
self._make_na_block(
placement=mgr_locs,
fill_value=fill_value,
use_na_proxy=use_na_proxy,
)
)

Not really sure what to do about this with adding a tonne of complexity (to already too complex code). I'm inclined to say it's OK to raise here, and that you should set your whole column:

df = pd.DataFrame({"x": [0, 1, 2, 3]})

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

df.loc[:, ["y", "z"]] = new_cols

pandas will already align on the index anyway, and you'll get the same result

@5j9
Copy link
Contributor Author

5j9 commented Nov 1, 2023

Thanks for investigating this issue.

I'm inclined to say it's OK to raise here, and that you should set your whole column

That's OK with me. It's just that the warning was a little vague.

pandas will already align on the index anyway, and you'll get the same result

If that's always the case, maybe pandas could raise another warning for assigning a dataframe using loc with specific rows specified in the indexer?

@5j9 5j9 closed this as completed Nov 1, 2023
@5j9 5j9 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

4 participants