Skip to content

Commit 29d7914

Browse files
committed
ENH: can pass an ndarray to DataFrame.join as the join key, GH #312
1 parent e63cbd7 commit 29d7914

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pandas/core/frame.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,9 +2209,11 @@ def join(self, other, on=None, how=None, lsuffix='', rsuffix=''):
22092209
----------
22102210
other : DataFrame
22112211
Index should be similar to one of the columns in this one
2212-
on : string, default None
2213-
Column name to use, otherwise join on index. Just like an Excel
2214-
VLOOKUP operation
2212+
on : column name, tuple/list of column names, or array-like
2213+
Column(s) to use for joining, otherwise join on index. If multiples
2214+
columns given, the passed DataFrame must have a MultiIndex. Can
2215+
pass an array as the join key if not already contained in the
2216+
calling DataFrame. Like an Excel VLOOKUP operation
22152217
how : {'left', 'right', 'outer', 'inner'}
22162218
How to handle indexes of the two objects. Default: 'left'
22172219
for joining on index, None otherwise
@@ -2250,6 +2252,8 @@ def _join_on(self, other, on, how, lsuffix, rsuffix):
22502252
join_key = zip(*[self[k] for k in on])
22512253
join_key = common._asarray_tuplesafe(join_key,
22522254
dtype=np.object_)
2255+
elif isinstance(on, np.ndarray) and len(on) == len(self):
2256+
join_key = on
22532257
else:
22542258
join_key = self[on].values
22552259

pandas/tests/test_frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,6 +3270,14 @@ def test_join_on(self):
32703270
source_copy['A'] = 0
32713271
self.assertRaises(Exception, target.join, source_copy, on='A')
32723272

3273+
def test_join_on_pass_vector(self):
3274+
expected = self.target.join(self.source, on='C')
3275+
del expected['C']
3276+
3277+
join_col = self.target.pop('C')
3278+
result = self.target.join(self.source, on=join_col)
3279+
assert_frame_equal(result, expected)
3280+
32733281
def test_join_with_len0(self):
32743282
# nothing to merge
32753283
merged = self.target.join(self.source.reindex([]), on='C')

0 commit comments

Comments
 (0)