Skip to content

Commit 4d689be

Browse files
committed
DOC: clarify Series alignment when assigning to DataFrame column (GH#39845)
1 parent 7863029 commit 4d689be

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/source/user_guide/indexing.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,26 @@ This is like an ``append`` operation on the ``DataFrame``.
766766
dfi.loc[3] = 5
767767
dfi
768768
769+
When assigning a :class:`Series` to a column, values are **aligned on index labels**.
770+
The order of the ``Series`` does not matter. Labels not present in the
771+
``DataFrame`` index result in ``NaN``.
772+
773+
.. ipython:: python
774+
775+
import pandas as pd
776+
777+
df = pd.DataFrame({"a": [1, 2, 3]})
778+
df
779+
780+
# assigning a partial Series aligns by index; other rows become NaN
781+
df["b"] = pd.Series({1: "b"})
782+
df
783+
784+
# order of the Series is irrelevant; labels drive the alignment
785+
s = pd.Series({2: "zero", 1: "one", 0: "two"})
786+
df["c"] = s
787+
df
788+
769789
.. _indexing.basics.get_value:
770790

771791
Fast scalar value getting and setting

0 commit comments

Comments
 (0)