-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG: Fix index order for Index.intersection()
closes #15582 Author: Albert Villanova del Moral <albert.villanova@gmail.com> Author: Jeff Reback <jeff@reback.net> Closes #15583 from albertvillanova/fix-15582 and squashes the following commits: 2d4e143 [Albert Villanova del Moral] Fix pytest fixture name collision 64e86a4 [Albert Villanova del Moral] Fix test on right join 73df69e [Albert Villanova del Moral] Address requested changes 8d2e9cc [Albert Villanova del Moral] Address requested changes 968c7f1 [Jeff Reback] DOC/TST: change to use parameterization 9e39794 [Albert Villanova del Moral] Address requested changes 5bf1508 [Albert Villanova del Moral] Address requested changes 654288b [Albert Villanova del Moral] Fix Travis errors 33eb740 [Albert Villanova del Moral] Address requested changes 3c200fe [Albert Villanova del Moral] Add new tests ef2581e [Albert Villanova del Moral] Fix Travis error f0d9d03 [Albert Villanova del Moral] Add whatsnew c96306d [Albert Villanova del Moral] Add sort argument to Index.join 047b513 [Albert Villanova del Moral] Address requested changes ec836bd [Albert Villanova del Moral] Fix Travis errors b977278 [Albert Villanova del Moral] Address requested changes 784fe75 [Albert Villanova del Moral] Fix error: line too long 1197b99 [Albert Villanova del Moral] Fix DataFrame column order when read from HDF file d9e29f8 [Albert Villanova del Moral] Create new DatetimeIndex from the Index.intersection result e7bcd28 [Albert Villanova del Moral] Fix typo in documentation a4ead99 [Albert Villanova del Moral] Fix typo c2a8dc3 [Albert Villanova del Moral] Implement tests c12bb3f [Albert Villanova del Moral] BUG: Fix index order for Index.intersection()
- Loading branch information
Showing
11 changed files
with
309 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,140 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
import numpy as np | ||
|
||
from pandas import DataFrame, Index | ||
from pandas.tests.frame.common import TestData | ||
import pandas.util.testing as tm | ||
|
||
|
||
@pytest.fixture | ||
def frame(): | ||
return TestData().frame | ||
|
||
|
||
@pytest.fixture | ||
def left(): | ||
return DataFrame({'a': [20, 10, 0]}, index=[2, 1, 0]) | ||
|
||
|
||
@pytest.fixture | ||
def right(): | ||
return DataFrame({'b': [300, 100, 200]}, index=[3, 1, 2]) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"how, sort, expected", | ||
[('inner', False, DataFrame({'a': [20, 10], | ||
'b': [200, 100]}, | ||
index=[2, 1])), | ||
('inner', True, DataFrame({'a': [10, 20], | ||
'b': [100, 200]}, | ||
index=[1, 2])), | ||
('left', False, DataFrame({'a': [20, 10, 0], | ||
'b': [200, 100, np.nan]}, | ||
index=[2, 1, 0])), | ||
('left', True, DataFrame({'a': [0, 10, 20], | ||
'b': [np.nan, 100, 200]}, | ||
index=[0, 1, 2])), | ||
('right', False, DataFrame({'a': [np.nan, 10, 20], | ||
'b': [300, 100, 200]}, | ||
index=[3, 1, 2])), | ||
('right', True, DataFrame({'a': [10, 20, np.nan], | ||
'b': [100, 200, 300]}, | ||
index=[1, 2, 3])), | ||
('outer', False, DataFrame({'a': [0, 10, 20, np.nan], | ||
'b': [np.nan, 100, 200, 300]}, | ||
index=[0, 1, 2, 3])), | ||
('outer', True, DataFrame({'a': [0, 10, 20, np.nan], | ||
'b': [np.nan, 100, 200, 300]}, | ||
index=[0, 1, 2, 3]))]) | ||
def test_join(left, right, how, sort, expected): | ||
|
||
result = left.join(right, how=how, sort=sort) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_join_index(frame): | ||
# left / right | ||
|
||
f = frame.loc[frame.index[:10], ['A', 'B']] | ||
f2 = frame.loc[frame.index[5:], ['C', 'D']].iloc[::-1] | ||
|
||
joined = f.join(f2) | ||
tm.assert_index_equal(f.index, joined.index) | ||
expected_columns = Index(['A', 'B', 'C', 'D']) | ||
tm.assert_index_equal(joined.columns, expected_columns) | ||
|
||
joined = f.join(f2, how='left') | ||
tm.assert_index_equal(joined.index, f.index) | ||
tm.assert_index_equal(joined.columns, expected_columns) | ||
|
||
joined = f.join(f2, how='right') | ||
tm.assert_index_equal(joined.index, f2.index) | ||
tm.assert_index_equal(joined.columns, expected_columns) | ||
|
||
# inner | ||
|
||
joined = f.join(f2, how='inner') | ||
tm.assert_index_equal(joined.index, f.index[5:10]) | ||
tm.assert_index_equal(joined.columns, expected_columns) | ||
|
||
# outer | ||
|
||
joined = f.join(f2, how='outer') | ||
tm.assert_index_equal(joined.index, frame.index.sort_values()) | ||
tm.assert_index_equal(joined.columns, expected_columns) | ||
|
||
tm.assertRaisesRegexp(ValueError, 'join method', f.join, f2, how='foo') | ||
|
||
# corner case - overlapping columns | ||
for how in ('outer', 'left', 'inner'): | ||
with tm.assertRaisesRegexp(ValueError, 'columns overlap but ' | ||
'no suffix'): | ||
frame.join(frame, how=how) | ||
|
||
|
||
def test_join_index_more(frame): | ||
af = frame.loc[:, ['A', 'B']] | ||
bf = frame.loc[::2, ['C', 'D']] | ||
|
||
expected = af.copy() | ||
expected['C'] = frame['C'][::2] | ||
expected['D'] = frame['D'][::2] | ||
|
||
result = af.join(bf) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = af.join(bf, how='right') | ||
tm.assert_frame_equal(result, expected[::2]) | ||
|
||
result = bf.join(af, how='right') | ||
tm.assert_frame_equal(result, expected.loc[:, result.columns]) | ||
|
||
|
||
def test_join_index_series(frame): | ||
df = frame.copy() | ||
s = df.pop(frame.columns[-1]) | ||
joined = df.join(s) | ||
|
||
# TODO should this check_names ? | ||
tm.assert_frame_equal(joined, frame, check_names=False) | ||
|
||
s.name = None | ||
tm.assertRaisesRegexp(ValueError, 'must have a name', df.join, s) | ||
|
||
|
||
def test_join_overlap(frame): | ||
df1 = frame.loc[:, ['A', 'B', 'C']] | ||
df2 = frame.loc[:, ['B', 'C', 'D']] | ||
|
||
joined = df1.join(df2, lsuffix='_df1', rsuffix='_df2') | ||
df1_suf = df1.loc[:, ['B', 'C']].add_suffix('_df1') | ||
df2_suf = df2.loc[:, ['B', 'C']].add_suffix('_df2') | ||
|
||
no_overlap = frame.loc[:, ['A', 'D']] | ||
expected = df1_suf.join(df2_suf).join(no_overlap) | ||
|
||
# column order not necessarily sorted | ||
tm.assert_frame_equal(joined, expected.loc[:, joined.columns]) |
Oops, something went wrong.