From b3b822fe77ea3f2a9de99f85afc5d5634ea01bc7 Mon Sep 17 00:00:00 2001 From: raj-thapa Date: Mon, 24 Jul 2023 12:23:14 +0000 Subject: [PATCH 1/2] #13950 - Updated documentation on pandas alignment when setting --- doc/source/user_guide/indexing.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index e785376ab10a4..13f73a94f0c28 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -155,7 +155,7 @@ columns. .. warning:: - pandas aligns all AXES when setting ``Series`` and ``DataFrame`` from ``.loc``, and ``.iloc``. + pandas aligns all AXES when setting ``Series`` and ``DataFrame`` from ``.loc``. This will **not** modify ``df`` because the column alignment is before value assignment. @@ -172,6 +172,16 @@ columns. df.loc[:, ['B', 'A']] = df[['A', 'B']].to_numpy() df[['A', 'B']] + However, pandas does not aligns AXES when setting ``Series`` and ``DataFrame`` from ``.iloc``. + + This will modify ``df`` because the column alignment is not done before value assignment. + + .. ipython:: python + + df[['A', 'B']] + df.iloc[:, [1, 0]] = df[['A', 'B']] + df[['A','B']] + Attribute access ---------------- From 198558936d7c3d3c45fe889f1012140ce043cf79 Mon Sep 17 00:00:00 2001 From: raj-thapa <118627384+raj-thapa@users.noreply.github.com> Date: Mon, 24 Jul 2023 12:33:57 -0500 Subject: [PATCH 2/2] Update doc/source/user_guide/indexing.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/user_guide/indexing.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index 13f73a94f0c28..b574ae9cb12c7 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -172,7 +172,8 @@ columns. df.loc[:, ['B', 'A']] = df[['A', 'B']].to_numpy() df[['A', 'B']] - However, pandas does not aligns AXES when setting ``Series`` and ``DataFrame`` from ``.iloc``. + However, pandas does not align AXES when setting ``Series`` and ``DataFrame`` from ``.iloc`` + because ``.iloc`` operates by position. This will modify ``df`` because the column alignment is not done before value assignment.