From c135abd98c9590cb68fb9c3765a493f7c6870a7f Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Mon, 2 May 2016 22:31:40 -0400 Subject: [PATCH 1/2] Add warning section in indexing docs. Close #12947. --- doc/source/indexing.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/source/indexing.rst b/doc/source/indexing.rst index 6227b08587790..9cf824b601733 100644 --- a/doc/source/indexing.rst +++ b/doc/source/indexing.rst @@ -191,6 +191,17 @@ raised. Multiple columns can also be set in this manner: You may find this useful for applying a transform (in-place) to a subset of the columns. +.. warning:: + + Pandas aligns index for assigning ``DataFrame`` from ``.loc``, ``.iloc`` and ``.ix``. + For example, in the expression + + .. code-block:: python + + df.loc[:,['B', 'A']] = df.loc[:,['A', 'B']] + + ``df`` is not modified. The alignment is ignored in column assignment. + Attribute Access ---------------- From 55ffdb1e63d9d785dd3859531dc4fe24bbfe1396 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Tue, 3 May 2016 10:12:53 -0400 Subject: [PATCH 2/2] Add warning section in indexing docs. For assigning part of a DataFrame, explain why the assignment may not work and then the correct way to do it. --- doc/source/indexing.rst | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/doc/source/indexing.rst b/doc/source/indexing.rst index 9cf824b601733..11a6fe1956f7c 100644 --- a/doc/source/indexing.rst +++ b/doc/source/indexing.rst @@ -193,14 +193,26 @@ columns. .. warning:: - Pandas aligns index for assigning ``DataFrame`` from ``.loc``, ``.iloc`` and ``.ix``. - For example, in the expression + pandas aligns index for setting a ``DataFrame`` from ``.loc``, ``.iloc`` and ``.ix``. + For example, the expression .. code-block:: python df.loc[:,['B', 'A']] = df.loc[:,['A', 'B']] - ``df`` is not modified. The alignment is ignored in column assignment. + will **not** modify ``df`` because index alignment is before value assignment. + + The correct way is to use raw values, and so this will work + + .. code-block:: python + + df.loc[:,['B', 'A']] = df.loc[:,['A', 'B']].values + + This will also work because pandas ignores index alignment in column assignment + + .. code-block:: python + + df[['B', 'A']] = df.loc[:,['A', 'B']] Attribute Access ----------------