Skip to content

DOC: improve docs for broadcasting behavior #18857

Open
@jakevdp

Description

@jakevdp

The following result was quite surprising to me:

>>> df = pd.DataFrame({'a': np.arange(3),
                       'b': np.ones(3)})
>>> df + [np.ones(3), np.ones(3)]
                 a                b
0  [1.0, 1.0, 1.0]  [2.0, 2.0, 2.0]
1  [2.0, 2.0, 2.0]  [2.0, 2.0, 2.0]
2  [3.0, 3.0, 3.0]  [2.0, 2.0, 2.0]

It looks like if you add a list to a dataframe, pandas casts it to a one-dimensional Series, where the entries of the series are array objects. The addition is then broadcasted down the columns such that each entry of the result is an array.

This behavior is quite counter-intuitive to me: is it a bug? If not, should it be documented?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions