From 36db164c7bee07d66b4335f051bac6a2ee60524a Mon Sep 17 00:00:00 2001 From: Marco Georgaklis Date: Fri, 18 Feb 2022 16:40:52 -0600 Subject: [PATCH 1/2] DOC: added examples for index assignment using Series --- pandas/core/indexing.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 941220f25e12f..988b5af5e0a75 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -449,6 +449,37 @@ def loc(self) -> _LocIndexer: 8 4 5 9 7 8 + **Set values with a Series** + + Set column values using a Series + + >>> df.loc[:, 'shield'] = pd.Series({7: 8, 8: 10, 9: 13}) + >>> df + max_speed shield + 7 1 8 + 8 4 10 + 9 7 13 + + Setting values using a Series with inconsistent indexes aligns the + right hand side to the index of the left + + >>> df.loc[:, 'shield'] = pd.Series({8: 10}) + >>> df + max_speed shield + 7 1 NaN + 8 4 10.0 + 9 7 NaN + + Setting value to Series with a missing index leaves the DataFrame + unchanged + + >>> df.loc[:, 'shield'] = pd.Series({11: 14}) + >>> df + max_speed shield + 7 1 NaN + 8 4 NaN + 9 7 NaN + **Getting values with a MultiIndex** A number of examples using a DataFrame with a MultiIndex From b241dc3bb0d57906c460bfa8055c5e122b17e2ab Mon Sep 17 00:00:00 2001 From: Marco Georgaklis Date: Thu, 24 Feb 2022 14:15:34 -0600 Subject: [PATCH 2/2] updated documentation for clarity --- pandas/core/indexing.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 988b5af5e0a75..eba579189d164 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -451,7 +451,8 @@ def loc(self) -> _LocIndexer: **Set values with a Series** - Set column values using a Series + Setting column values using a Series aligns along the index values of + the Series and DataFrame >>> df.loc[:, 'shield'] = pd.Series({7: 8, 8: 10, 9: 13}) >>> df @@ -460,8 +461,8 @@ def loc(self) -> _LocIndexer: 8 4 10 9 7 13 - Setting values using a Series with inconsistent indexes aligns the - right hand side to the index of the left + Where the index values are not found in the Series index, the values + will be NaN >>> df.loc[:, 'shield'] = pd.Series({8: 10}) >>> df