Skip to content

Commit

Permalink
Allow Table.join to work with column ids. wireservice/csvkit#711
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Dec 26, 2016
1 parent 4d4c342 commit 0c09a6c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.5.3
-----

* :meth:`.Table.join` can now accept column indicies instead of column names.
* :meth:`.Table.from_csv` now buffers input files to prevent issues with using STDIN as an input.

1.5.2 - December 24, 2016
Expand Down
14 changes: 7 additions & 7 deletions agate/table/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def join(self, right_table, left_key, right_key=None, inner=False, require_match
:param right_table:
The "right" table to join to.
:param left_key:
Either the name of a column from the this table to join on, a
sequence of such column names, or a :class:`function` that takes a
row and returns a value to join on.
Either the name of a column from the this table to join on, the index
of a column, a sequence of such column identifiers, or a
:class:`function` that takes a row and returns a value to join on.
:param right_key:
Either the name of a column from :code:table` to join on, a
sequence of such column names, or a :class:`function` that takes a
row and returns a value to join on. If :code:`None` then
Either the name of a column from :code:table` to join on, the index of
a column, a sequence of such column identifiers, or a :class:`function`
that takes a ow and returns a value to join on. If :code:`None` then
:code:`left_key` will be used for both.
:param inner:
Perform a SQL-style "inner join" instead of a left outer join. Rows
Expand Down Expand Up @@ -83,7 +83,7 @@ def join(self, right_table, left_key, right_key=None, inner=False, require_match
else:
right_column = right_table._columns[right_key]
right_data = right_column.values()
right_key_indices = [right_table._columns._keys.index(right_key)]
right_key_indices = [right_table._columns.index(right_column)]

# Build names and type lists
column_names = list(self._column_names)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_table/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ def test_join(self):
(None, 2, 'c', 2, 'c')
])

def test_join_column_indicies(self):
new_table = self.left.join(self.right, 0, 0)

self.assertIsNot(new_table, self.left)
self.assertIsNot(new_table, self.right)
self.assertColumnNames(new_table, ['one', 'two', 'three', 'five', 'six'])
self.assertColumnTypes(new_table, [Number, Number, Text, Number, Text])
self.assertRows(new_table, [
(1, 4, 'a', 4, 'a'),
(2, 3, 'b', 3, 'b'),
(None, 2, 'c', 2, 'c')
])

def test_join_match_multiple(self):
left_rows = (
(1, 4, 'a'),
Expand Down

0 comments on commit 0c09a6c

Please sign in to comment.