Skip to content

DOC: indexing with alignment #15508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,95 @@
merge_ordered
merge_asof


pandas aligns index for setting a ``DataFrame`` from ``.loc``, ``.iloc`` and ``.ix``.

Therefore, any attempt to assign copies from one Dataframe
to another without matching indices will result in NaN in the assigned rows.

What we would like to do:
.. code-block:: python

In [1]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [2]: a.iloc[0:2,:] = a.iloc[3:5, :]
Out [3]: a

Expected outcome:

a b
0 4 9
1 5 10
2 3 8
3 4 9
4 5 10

Actual outcome:

a b
0 NaN NaN
1 NaN NaN
2 3.0 8.0
3 4.0 9.0
4 5.0 10.0


Currently, the assignment operator behaves similar to below because the indices
do not match.

.. code-block:: python

In [1]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [2]: values = pd.DataFrame({'a': [4, 5], 'b': [9, 10]}, index=[3, 4])
In [3]: a.iloc[0:2,:]
Out [4]: a

a b
0 NaN NaN
1 NaN NaN
2 3.0 8.0
3 4.0 9.0
4 5.0 10.0


However, it will work if indices match or are omitted from rhs
of assignment operator.

.. code-block:: python

In [1]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [2]: values = pd.DataFrame({'a': [4, 5], 'b': [9, 10]}, index=[0, 1])
In [3]: a.iloc[0:2,:] = values
Out [4]: a


a b
0 4 9
1 5 10
2 3 8
3 4 9
4 5 10


Hence, when performing the operation, create the shape you want on the rhs
and convert it to a non-PANDAS DataFrame structure, such as converting
the values into a numpy.ndarray


.. code-block:: python

In [1]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [2]: values = pd.DataFrame({'a': [4, 5], 'b': [9, 10]}, index=[1, 2])
In [3]: a.iloc[0:2,:] = a.iloc[3:5, :].values
Out [4]: a

a b
0 4 9
1 5 10
2 3 8
3 4 9
4 5 10


"""

# -----------------------------------------------------------------------
Expand Down