Skip to content

Commit d78266e

Browse files
nbonnottejreback
authored andcommitted
BUG: df.join(df2, how='right') TypeError: Argument 'left' has incorrect type (issue #11519)
1 parent 9cbe8b9 commit d78266e

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ Bug Fixes
127127

128128
- Bug in the link-time error caused by C ``inline`` functions on FreeBSD 10+ (with ``clang``) (:issue:`10510`)
129129
- Bug in ``DataFrame.to_csv`` in passing through arguments for formatting ``MultiIndexes``, including ``date_format`` (:issue:`7791`)
130+
- Bug in ``DataFrame.join()`` with ``how='right'`` producing a ``TypeError`` (:issue:`11519`)

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,7 @@ def _join_monotonic(self, other, how='left', return_indexers=False):
24902490
if how == 'left':
24912491
join_index, lidx, ridx = self._left_indexer(sv, ov)
24922492
elif how == 'right':
2493-
join_index, ridx, lidx = self._left_indexer(other, self)
2493+
join_index, ridx, lidx = self._left_indexer(ov, sv)
24942494
elif how == 'inner':
24952495
join_index, lidx, ridx = self._inner_indexer(sv, ov)
24962496
elif how == 'outer':

pandas/tests/test_index.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ def test_join_self(self):
16821682
for kind in kinds:
16831683
joined = res.join(res, how=kind)
16841684
self.assertIs(res, joined)
1685+
16851686
def test_str_attribute(self):
16861687
# GH9068
16871688
methods = ['strip', 'rstrip', 'lstrip']
@@ -3056,17 +3057,15 @@ def test_join_left(self):
30563057
tm.assert_numpy_array_equal(ridx, eridx)
30573058

30583059
# non-unique
3059-
"""
3060-
idx = Index([1,1,2,5])
3061-
idx2 = Index([1,2,5,7,9])
3060+
idx = Index([1, 1, 2, 5])
3061+
idx2 = Index([1, 2, 5, 7, 9])
30623062
res, lidx, ridx = idx2.join(idx, how='left', return_indexers=True)
3063-
eres = idx2
3064-
eridx = np.array([0, 2, 3, -1, -1])
3065-
elidx = np.array([0, 1, 2, 3, 4])
3063+
eres = Index([1, 1, 2, 5, 7, 9]) # 1 is in idx2, so it should be x2
3064+
eridx = np.array([0, 1, 2, 3, -1, -1])
3065+
elidx = np.array([0, 0, 1, 2, 3, 4])
30663066
self.assertTrue(res.equals(eres))
30673067
tm.assert_numpy_array_equal(lidx, elidx)
30683068
tm.assert_numpy_array_equal(ridx, eridx)
3069-
"""
30703069

30713070
def test_join_right(self):
30723071
other = Int64Index([7, 12, 25, 1, 2, 5])
@@ -3096,24 +3095,16 @@ def test_join_right(self):
30963095
self.assertIsNone(ridx)
30973096

30983097
# non-unique
3099-
"""
3100-
idx = Index([1,1,2,5])
3101-
idx2 = Index([1,2,5,7,9])
3098+
idx = Index([1, 1, 2, 5])
3099+
idx2 = Index([1, 2, 5, 7, 9])
31023100
res, lidx, ridx = idx.join(idx2, how='right', return_indexers=True)
3103-
eres = idx2
3104-
elidx = np.array([0, 2, 3, -1, -1])
3105-
eridx = np.array([0, 1, 2, 3, 4])
3101+
eres = Index([1, 1, 2, 5, 7, 9]) # 1 is in idx2, so it should be x2
3102+
elidx = np.array([0, 1, 2, 3, -1, -1])
3103+
eridx = np.array([0, 0, 1, 2, 3, 4])
31063104
self.assertTrue(res.equals(eres))
31073105
tm.assert_numpy_array_equal(lidx, elidx)
31083106
tm.assert_numpy_array_equal(ridx, eridx)
31093107

3110-
idx = Index([1,1,2,5])
3111-
idx2 = Index([1,2,5,9,7])
3112-
res = idx.join(idx2, how='right', return_indexers=False)
3113-
eres = idx2
3114-
self.assert(res.equals(eres))
3115-
"""
3116-
31173108
def test_join_non_int_index(self):
31183109
other = Index([3, 6, 7, 8, 10], dtype=object)
31193110

pandas/tools/tests/test_merge.py

+17
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,23 @@ def test_join_many_non_unique_index(self):
517517

518518
assert_frame_equal(result, expected.ix[:, result.columns])
519519

520+
# GH 11519
521+
df = DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
522+
'foo', 'bar', 'foo', 'foo'],
523+
'B': ['one', 'one', 'two', 'three',
524+
'two', 'two', 'one', 'three'],
525+
'C': np.random.randn(8),
526+
'D': np.random.randn(8)})
527+
s = Series(np.repeat(np.arange(8), 2),
528+
index=np.repeat(np.arange(8), 2), name='TEST')
529+
inner = df.join(s, how='inner')
530+
outer = df.join(s, how='outer')
531+
left = df.join(s, how='left')
532+
right = df.join(s, how='right')
533+
assert_frame_equal(inner, outer)
534+
assert_frame_equal(inner, left)
535+
assert_frame_equal(inner, right)
536+
520537
def test_merge_index_singlekey_right_vs_left(self):
521538
left = DataFrame({'key': ['a', 'b', 'c', 'd', 'e', 'e', 'a'],
522539
'v1': np.random.randn(7)})

0 commit comments

Comments
 (0)