Skip to content

Commit f9ade87

Browse files
add warning section in indexing doc.
replace PR #13060 and close #12947.
1 parent 87b0f4d commit f9ade87

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

doc/source/indexing.rst

+25
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ raised. Multiple columns can also be set in this manner:
191191
You may find this useful for applying a transform (in-place) to a subset of the
192192
columns.
193193

194+
.. warning::
195+
196+
pandas aligns index when setting ``Series`` and ``DataFrame`` from ``.loc``, ``.iloc`` and ``.ix``.
197+
198+
This will **not** modify ``df`` because index alignment is before value assignment.
199+
200+
.. ipython:: python
201+
202+
df[['A', 'B']]
203+
df.loc[:,['B', 'A']] = df[['A', 'B']]
204+
df[['A', 'B']]
205+
206+
The correct way is to use raw values
207+
208+
.. ipython:: python
209+
210+
df.loc[:,['B', 'A']] = df[['A', 'B']].values
211+
df[['A', 'B']]
212+
213+
This will also work because pandas ignores index alignment in column assignment
214+
215+
.. code-block:: python
216+
217+
df[['B', 'A']] = df[['A', 'B']]
218+
194219
Attribute Access
195220
----------------
196221

0 commit comments

Comments
 (0)