Skip to content

Assignment to multiple columns only works if they existed before #13658

Closed
@jzwinck

Description

@jzwinck
Assignment to multiple columns of a :class:`DataFrame` when some of the columns do not exist would previously assign the values to the last column. Now, new columns would be constructed with the right values. 

.. ipython:: python

   df = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]})
   df

*Previous behavior*:

.. code-block:: ipython

   In [3]: df[['a', 'c']] = 1
   In [4]: df
   Out[4]:
      a  b
   0  1  1
   1  1  1
   2  1  1

*New behavior*:

.. ipython:: python

   df[['a', 'c']] = 1
   df

import pandas as pd
df = pd.DataFrame({'a': [1, 2]})
df['b'] = 3 # creates column 'b'
df[['a', 'b']] = 4 # assigns to columns 'a' and 'b'
df[['c', 'd']] = 5 # KeyError: "['c' 'd'] not in index" !
df[['b', 'c']] = 6 # KeyError: "['c'] not in index" !

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.

INSTALLED VERSIONS
python: 3.5.1.final.0
python-bits: 64
pandas: 0.18.1
numpy: 1.11.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions