-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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: setting item to iterable with .at fails when column doesn't exist or has wrong dtype #61223
Comments
The same errors happen using A workaround is specifying the column first: |
Looking at the documentation it seems like this example does not show a bug, since it should throw a KeyError given there is no key "C" already in the dataframe. Moreover, the .at function is specifically used "if you only need to get or set a single value in a DataFrame or Series." This is why it works when we convert the dataframe to a object dtype. The real bug here seems to be that it works when providing a scalar: it should throw a KeyError. If there is a need to fix .at to throw an error I would be happy to work on it as my first issue. |
@ShayanG9 The documentation says it should throw KeyError when getting, not setting. Userguide specificaly states that it will inflate the dataframe inplace if the key does not exist. All documentation says it should work the same as .loc, just access only one cell of the dataframe. @yuanx749 That's the issue, if you look at the trace, it falls back to .loc, which throws this error. But it shouldn't fall back to .loc, it should access a single cell and put a list to it. |
It seems like you are right about the KeyError. However, given it should change only one cell then it doesn't make sense to exchange a cell populated by an integer type with a list, like you mention in the line import pandas as pd
df=pd.DataFrame(data=[[1,2],[3,4]],index=["a","b"],columns=["A","B"])
print(df.dtypes) returns
However, if we do
We would get
Now that the series is of dtype object we can replace the int64 object with a list object, since they are interchangeable. I'm not sure if there is something I might be missing, but the implementation seems to make sense. Perhaps a note about this behavior in the documentation would be good? |
What you propose would be incompatible with .loc When you assign incompatible value with .loc, it will throw a future warning, but it will change the dtype to the compatible one.
Btw. if you do this example with |
Thanks for clarifying, and sorry for the misunderstanding. I see what you mean now. Looking at this pull request it seems like this behavior is intentional. If you look it seems like it was previously in the code that a more descriptive error would be thrown: |
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
When using .at to set cell value to an iterable, it fails if it has to create a new column.
It works fine if setting the cell value to a scalar (like
df.at["a","C"]=1
).It also fails if the column exists but is of the wrong dtype.
This fails:
df.at["a","A"]=[1,2,3]
But this works:
The error trace:
Expected Behavior
The cell value is set without errors.
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.9.21
python-bits : 64
OS : Darwin
OS-release : 24.3.0
Version : Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 2.2.3
numpy : 2.0.2
pytz : 2025.1
dateutil : 2.8.2
pip : 25.0.1
Cython : None
sphinx : None
IPython : 8.18.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.6.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 5.3.1
matplotlib : 3.9.4
numba : None
numexpr : 2.10.2
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 18.1.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : 2023.6.0
scipy : 1.13.1
sqlalchemy : None
tables : N/A
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: