-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
BUG: Fix index order for Index.intersection() #15583
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
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c12bb3f
BUG: Fix index order for Index.intersection()
c2a8dc3
Implement tests
a4ead99
Fix typo
e7bcd28
Fix typo in documentation
d9e29f8
Create new DatetimeIndex from the Index.intersection result
1197b99
Fix DataFrame column order when read from HDF file
784fe75
Fix error: line too long
b977278
Address requested changes
ec836bd
Fix Travis errors
047b513
Address requested changes
c96306d
Add sort argument to Index.join
f0d9d03
Add whatsnew
ef2581e
Fix Travis error
3c200fe
Add new tests
33eb740
Address requested changes
654288b
Fix Travis errors
5bf1508
Address requested changes
9e39794
Address requested changes
968c7f1
DOC/TST: change to use parameterization
jreback 8d2e9cc
Address requested changes
73df69e
Address requested changes
64e86a4
Fix test on right join
2d4e143
Fix pytest fixture name collision
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add new tests
- Loading branch information
commit 3c200fe734423e0c5c58fc9c1e198d52022b200c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import print_function | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestDataFrameMerge(object): | ||
|
||
def test_merge_on_indexes(self): | ||
df1 = pd.DataFrame({'a': [20, 10, 0]}, index=[2, 1, 0]) | ||
df2 = pd.DataFrame({'b': [100, 200, 300]}, index=[1, 2, 3]) | ||
|
||
# default how='inner' | ||
result = df1.merge(df2, left_index=True, right_index=True) | ||
expected = pd.DataFrame({'a': [20, 10], 'b': [200, 100]}, | ||
index=[2, 1]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='left' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='left') | ||
expected = pd.DataFrame({'a': [20, 10, 0], 'b': [200, 100, np.nan]}, | ||
index=[2, 1, 0]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='right' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='right') | ||
expected = pd.DataFrame({'a': [10, 20, np.nan], 'b': [100, 200, 300]}, | ||
index=[1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='inner' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='inner') | ||
expected = pd.DataFrame({'a': [20, 10], 'b': [200, 100]}, | ||
index=[2, 1]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='outer' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='outer') | ||
expected = pd.DataFrame({'a': [0, 10, 20, np.nan], | ||
'b': [np.nan, 100, 200, 300]}, | ||
index=[0, 1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_merge_on_indexes_sort(self): | ||
df1 = pd.DataFrame({'a': [20, 10, 0]}, index=[2, 1, 0]) | ||
df2 = pd.DataFrame({'b': [100, 200, 300]}, index=[1, 2, 3]) | ||
|
||
# default how='inner' | ||
result = df1.merge(df2, left_index=True, right_index=True, sort=True) | ||
expected = pd.DataFrame({'a': [10, 20], 'b': [100, 200]}, | ||
index=[1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='left' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='left', sort=True) | ||
expected = pd.DataFrame({'a': [0, 10, 20], 'b': [np.nan, 100, 200]}, | ||
index=[0, 1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='right' (already sorted) | ||
result = df1.merge(df2, left_index=True, right_index=True, how='right', sort=True) | ||
expected = pd.DataFrame({'a': [10, 20, np.nan], 'b': [100, 200, 300]}, | ||
index=[1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='right' | ||
result = df2.merge(df1, left_index=True, right_index=True, how='right', sort=True) | ||
expected = pd.DataFrame([[np.nan, 0], [100, 10], [200, 20]], | ||
columns=['b', 'a'], index=[0, 1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='inner' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='inner', sort=True) | ||
expected = pd.DataFrame({'a': [10, 20], 'b': [100, 200]}, | ||
index=[1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='outer' | ||
result = df1.merge(df2, left_index=True, right_index=True, how='outer', sort=True) | ||
expected = pd.DataFrame({'a': [0, 10, 20, np.nan], | ||
'b': [np.nan, 100, 200, 300]}, | ||
index=[0, 1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1355,3 +1355,81 @@ def test_dtype_on_merged_different(self, change, how, left, right): | |
np.dtype('int64')], | ||
index=['X', 'Y', 'Z']) | ||
assert_series_equal(result, expected) | ||
|
||
class TestMergeOnIndexes(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok you added here good. |
||
|
||
def test_merge_on_indexes(self): | ||
df1 = pd.DataFrame({'a': [20, 10, 0]}, index=[2, 1, 0]) | ||
df2 = pd.DataFrame({'b': [100, 200, 300]}, index=[1, 2, 3]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change this to be parametrized like join above. (e.g. matrix of how/sort) |
||
# default how='inner' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True) | ||
expected = pd.DataFrame({'a': [20, 10], 'b': [200, 100]}, | ||
index=[2, 1]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='left' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='left') | ||
expected = pd.DataFrame({'a': [20, 10, 0], 'b': [200, 100, np.nan]}, | ||
index=[2, 1, 0]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='right' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='right') | ||
expected = pd.DataFrame({'a': [10, 20, np.nan], 'b': [100, 200, 300]}, | ||
index=[1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='inner' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='inner') | ||
expected = pd.DataFrame({'a': [20, 10], 'b': [200, 100]}, | ||
index=[2, 1]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='outer' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='outer') | ||
expected = pd.DataFrame({'a': [0, 10, 20, np.nan], | ||
'b': [np.nan, 100, 200, 300]}, | ||
index=[0, 1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_merge_on_indexes_sort(self): | ||
df1 = pd.DataFrame({'a': [20, 10, 0]}, index=[2, 1, 0]) | ||
df2 = pd.DataFrame({'b': [100, 200, 300]}, index=[1, 2, 3]) | ||
|
||
# default how='inner' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, sort=True) | ||
expected = pd.DataFrame({'a': [10, 20], 'b': [100, 200]}, | ||
index=[1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='left' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='left', sort=True) | ||
expected = pd.DataFrame({'a': [0, 10, 20], 'b': [np.nan, 100, 200]}, | ||
index=[0, 1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='right' (already sorted) | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='right', sort=True) | ||
expected = pd.DataFrame({'a': [10, 20, np.nan], 'b': [100, 200, 300]}, | ||
index=[1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='right' | ||
result = pd.merge(df2, df1, left_index=True, right_index=True, how='right', sort=True) | ||
expected = pd.DataFrame([[np.nan, 0], [100, 10], [200, 20]], | ||
columns=['b', 'a'], index=[0, 1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='inner' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='inner', sort=True) | ||
expected = pd.DataFrame({'a': [10, 20], 'b': [100, 200]}, | ||
index=[1, 2]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# how='outer' | ||
result = pd.merge(df1, df2, left_index=True, right_index=True, how='outer', sort=True) | ||
expected = pd.DataFrame({'a': [0, 10, 20, np.nan], | ||
'b': [np.nan, 100, 200, 300]}, | ||
index=[0, 1, 2, 3]) | ||
tm.assert_frame_equal(result, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, these need to go in
pandas/tests/tools/test_merge.py
. just fit them in where appropriate..join
is fine here as its a direct / used method on DataFrame..merge
is a direct calling of thepd.merge
where LOTS of tests already exist. Test location is very important to avoid future confusion.In fact I suspect most of these are duplicated tests, pls only add as appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you already added to the correct location. no point in duplicating tests. delete these.