-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Assignment to multiple columns only works if they existed before #13658
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
In current master (0.25.0.dev0+564.g9c5165e26) multiple-column assignment gives the following error when none of the columns exists: import pandas as pd
df = pd.DataFrame({'a': [1, 2]})
df[['b', 'c']] = 1
However a warning is given instead when only some of the column do not exist: df[['a', 'c']] = 1
This seems quite inconsistent. Should it be fixed? @jreback |
@jreback any updates? |
this is an open issue @howsiwei |
@jreback I'm curious what's the intended behavior and if PR is welcomed. |
@howsiwei likely this is a bug |
@jreback do you mean that multiple-column assignment should only gives a warning when none of the columns exists? |
multiple column with a scalar assignment should work w/o a warning or error |
Hi, I have the same issue with multiple rows assignment: Pandas version 2.4.1 df=pd.DataFrame({'a':[1,2,3,4],'b':[5,6,7,8]}) Traceback (most recent call last): Cell In[97], line 1
File ~..\lib\site-packages\pandas\core\indexing.py:881 in setitem
File ~..\lib\site-packages\pandas\core\indexing.py:764 in _get_setitem_indexer
File ~..\lib\site-packages\pandas\core\indexing.py:1484 in _convert_to_indexer
File ~..\lib\site-packages\pandas\core\indexing.py:1520 in _get_listlike_indexer
File ~..\lib\site-packages\pandas\core\indexes\base.py:6115 in _get_indexer_strict
File ~..\lib\site-packages\pandas\core\indexes\base.py:6176 in _raise_if_missing
KeyError: "None of [Index([4, 5], dtype='int32')] are in the [index]" works for columns: Out[101]: a b c d 0 0 0 0 0 1 0 0 0 0 2 3 7 0 0 3 4 8 0 0 Is there an easy way to update values for certain indices when some of them exist and some new? |
Uh oh!
There was an error while loading. Please reload this page.
I would have expected all the above cases to work, but for some reason the last two fail. New column creation only seems to work for a single column, whilst multiple-column assignment works only if all columns exist.
The text was updated successfully, but these errors were encountered: