Skip to content

Commit 8d2e9cc

Browse files
author
Albert Villanova del Moral
committed
Address requested changes
1 parent 968c7f1 commit 8d2e9cc

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

doc/source/whatsnew/v0.20.0.txt

+2-9
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ New Behavior:
740740

741741
.. _whatsnew_0200.api_breaking.index_order:
742742

743-
Index order after inner join due to Index intersection
744-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
743+
Index.intersection and inner join now preserve the order of the left Index
744+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
745745

746746
The ``Index.intersection`` now preserves the order of the calling Index (left)
747747
instead of the other Index (right) (:issue:`15582`). This affects the inner
@@ -788,18 +788,11 @@ joins (methods ``DataFrame.join`` and ``pd.merge``) and the .align methods.
788788
1 10 100
789789
2 20 200
790790

791-
In [5]: pd.merge(df1, df2, how='inner', left_index=True, right_index=True)
792-
Out[5]:
793-
a b
794-
1 10 100
795-
2 20 200
796-
797791
New Behavior:
798792

799793
.. ipython:: python
800794

801795
df1.join(df2, how='inner')
802-
pd.merge(df1, df2, how='inner', left_index=True, right_index=True)
803796

804797

805798
.. _whatsnew_0200.api:

pandas/core/frame.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@
124124
----------%s
125125
right : DataFrame
126126
how : {'left', 'right', 'outer', 'inner'}, default 'inner'
127-
* left: use only keys from left frame (SQL: left outer join)
128-
* right: use only keys from right frame (SQL: right outer join)
129-
* outer: use union of keys from both frames (SQL: full outer join)
127+
* left: use only keys from left frame (SQL: left outer join), preserving
128+
their order
129+
* right: use only keys from right frame (SQL: right outer join), preserving
130+
their order
131+
* outer: use union of keys from both frames (SQL: full outer join), and
132+
sort them lexicographically
130133
* inner: use intersection of keys from both frames (SQL: inner join),
131134
preserving the order of the left keys
132135
on : label or list

pandas/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -2844,6 +2844,8 @@ def _reindex_non_unique(self, target):
28442844
level : int or level name, default None
28452845
return_indexers : boolean, default False
28462846
sort : boolean, default False
2847+
Sort the join keys lexicographically in the result Index. If False,
2848+
the order of the join keys depends on the join type (how keyword)
28472849
28482850
.. versionadded:: 0.20.0
28492851

pandas/tests/frame/test_join.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def df1():
2020

2121
@pytest.fixture
2222
def df2():
23-
return DataFrame({'b': [100, 200, 300]}, index=[1, 2, 3])
23+
return DataFrame({'b': [300, 100, 200]}, index=[3, 1, 2])
2424

2525

2626
@pytest.mark.parametrize(
@@ -37,8 +37,8 @@ def df2():
3737
('left', True, DataFrame({'a': [0, 10, 20],
3838
'b': [np.nan, 100, 200]},
3939
index=[0, 1, 2])),
40-
('right', False, DataFrame({'a': [10, 20, np.nan],
41-
'b': [100, 200, 300]},
40+
('right', False, DataFrame({'a': [np.nan, 10, 20],
41+
'b': [300, 100, 200]},
4242
index=[1, 2, 3])),
4343
('right', True, DataFrame({'a': [10, 20, np.nan],
4444
'b': [100, 200, 300]},

0 commit comments

Comments
 (0)