From c69bc1ec270cb35ed3c8ee0dadad4cba9c330482 Mon Sep 17 00:00:00 2001
From: Peter Steinbach
Date: Fri, 29 May 2020 17:26:28 +0200
Subject: [PATCH 1/4] mask based multi-index assignment of column values
described
---
doc/source/user_guide/indexing.rst | 28 +++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst
index 6db757e726792..4cdc906ebea34 100644
--- a/doc/source/user_guide/indexing.rst
+++ b/doc/source/user_guide/indexing.rst
@@ -1870,25 +1870,35 @@ This is the correct access method:
.. ipython:: python
- dfc = pd.DataFrame({'A': ['aaa', 'bbb', 'ccc'], 'B': [1, 2, 3]})
- dfc.loc[0, 'A'] = 11
- dfc
+ dfc = pd.DataFrame({'a': ['one', 'one', 'two',
+ 'three', 'two', 'one', 'six'],
+ 'c': np.arange(7)})
+ dfd = dfc.copy()
+
+ # Setting multiple items using a mask (recommended)
+ mask = dfd['a'].str.startswith('o')
+ dfd.loc[mask, 'c'] = 42
+ dfd
+
+ # Setting a single item (recommended)
+ dfd.loc[2, 'a'] = 11
+ dfd
-This *can* work at times, but it is not guaranteed to, and therefore should be avoided:
+The following *can* work at times, but it is not guaranteed to, and therefore should be avoided:
.. ipython:: python
:okwarning:
- dfc = dfc.copy()
- dfc['A'][0] = 111
- dfc
+ dfd = dfc.copy()
+ dfd['a'][2] = 111
+ dfd
-This will **not** work at all, and so should be avoided:
+Last, the subsequent example will **not** work at all, and so should be avoided:
::
>>> pd.set_option('mode.chained_assignment','raise')
- >>> dfc.loc[0]['A'] = 1111
+ >>> dfd.loc[0]['a'] = 1111
Traceback (most recent call last)
...
SettingWithCopyException:
From 1bd5d9daa4add2f10bbb959d7e5a6b42df191532 Mon Sep 17 00:00:00 2001
From: Peter Steinbach
Date: Tue, 2 Jun 2020 09:46:34 +0200
Subject: [PATCH 2/4] removing blank line
---
doc/source/user_guide/indexing.rst | 1 -
1 file changed, 1 deletion(-)
diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst
index 4cdc906ebea34..8377b11b6828e 100644
--- a/doc/source/user_guide/indexing.rst
+++ b/doc/source/user_guide/indexing.rst
@@ -1874,7 +1874,6 @@ This is the correct access method:
'three', 'two', 'one', 'six'],
'c': np.arange(7)})
dfd = dfc.copy()
-
# Setting multiple items using a mask (recommended)
mask = dfd['a'].str.startswith('o')
dfd.loc[mask, 'c'] = 42
From de9f6bd15c39a36ed914192504725f85da34aefe Mon Sep 17 00:00:00 2001
From: Peter Steinbach
Date: Tue, 2 Jun 2020 16:03:22 +0200
Subject: [PATCH 3/4] resetting dataframe for clarity
---
doc/source/user_guide/indexing.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst
index 8377b11b6828e..d2d60948854f2 100644
--- a/doc/source/user_guide/indexing.rst
+++ b/doc/source/user_guide/indexing.rst
@@ -1880,6 +1880,7 @@ This is the correct access method:
dfd
# Setting a single item (recommended)
+ dfd = dfc.copy()
dfd.loc[2, 'a'] = 11
dfd
From 5aa53c693cc947f02e8a9050257d8b5c973a8768 Mon Sep 17 00:00:00 2001
From: Peter Steinbach
Date: Tue, 2 Jun 2020 16:05:21 +0200
Subject: [PATCH 4/4] making the recommended way explicit
---
doc/source/user_guide/indexing.rst | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst
index d2d60948854f2..6843dd1eadc81 100644
--- a/doc/source/user_guide/indexing.rst
+++ b/doc/source/user_guide/indexing.rst
@@ -1866,7 +1866,7 @@ A chained assignment can also crop up in setting in a mixed dtype frame.
These setting rules apply to all of ``.loc/.iloc``.
-This is the correct access method:
+The following is the recommended access method using ``.loc`` for multiple items (using ``mask``) and a single item using a fixed index:
.. ipython:: python
@@ -1874,12 +1874,12 @@ This is the correct access method:
'three', 'two', 'one', 'six'],
'c': np.arange(7)})
dfd = dfc.copy()
- # Setting multiple items using a mask (recommended)
+ # Setting multiple items using a mask
mask = dfd['a'].str.startswith('o')
dfd.loc[mask, 'c'] = 42
dfd
- # Setting a single item (recommended)
+ # Setting a single item
dfd = dfc.copy()
dfd.loc[2, 'a'] = 11
dfd