-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: FutureWarning appears when assigning new column through loc #55791
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
Comments
cc @MarcoGorelli. |
Small contribution. A minimal, reproducible example, giving an almost identical df = pd.DataFrame({"a": [1]})
df.loc[0, "b"] = True The only difference is that instead of indicating that print(df["b"].dtype) Returns |
Thanks - indeed, the dtype of the column created here is In [9]: df = pd.DataFrame({"a": [1]})
...: df.loc[0, "b"] = True
<ipython-input-9-44fb3a4bf8be>:2: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value 'True' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
df.loc[0, "b"] = True
In [10]: df.dtypes
Out[10]:
a int64
b object
dtype: object You should instead do In [11]: df = pd.DataFrame({"a": [1]})
...: df.loc[:, "b"] = True
In [12]: df.dtypes
Out[12]:
a int64
b bool
dtype: object and then you'll get a This is exactly what the deprecation warning is meant to address Still, would be nice to be able to do this without the upcasting happening at all, just wondering how to do it without further spaghettifying logic culprit is pandas/pandas/core/indexing.py Line 875 in 0e2277b
|
To give a longer explanation - what's the difference between
The difference is pandas/pandas/core/indexing.py Lines 860 to 876 in 0e2277b
In the first case, Then, they get to pandas/pandas/core/indexing.py Lines 2101 to 2118 in 0e2277b
The first case is a full setter, the second one isn't. So, they both attempt
So, in the first place, the same warning should probably be raised as well As for what to do about not warning when expanding, I don't know yet |
Thanks for the explanation @MarcoGorelli. |
Problem is, we can't just raise within the df = DataFrame({"a": [1, 2], "b": [3, 4]})
df.loc[:, "a"] = {1: 100, 0: 200} also goes there This extreme flexibility comes at a cost unfortunately, will see what can be done Also, came across #56024 whilst investigating |
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
Issue Description
The code above gives
FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[ True]' has dtype incompatible with float64, please explicitly cast to a compatible dtype first. df1.loc[df2.index, df2.columns] = df
where I do not expect any warning at all. A new column is created without prior dtype defined.
Expected Behavior
No FutureWarning raised.
Installed Versions
INSTALLED VERSIONS
commit : a60ad39
python : 3.11.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-87-generic
Version : #97~20.04.1-Ubuntu SMP Thu Oct 5 08:25:28 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.2
numpy : 1.26.0
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.2.1
Cython : None
pytest : 7.4.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : 1.3.7
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: