Skip to content

Commit

Permalink
Added a row_names arg to Table.merge. Closes wireservice#403.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Jan 2, 2016
1 parent d8a2312 commit 6dbb45d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.1.1
-----

* :meth:`.Table.merge` now accepts a ``row_names`` argument. (#403)
* :class:`.Formula` now automatically casts computed values to specified data type unless ``invalidate`` is set to ``False``. (#398)
* Added Neil Bedi to AUTHORS.
* :meth:`.Table.rename` is implemented. (#389)
Expand Down
14 changes: 10 additions & 4 deletions agate/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from agate.aggregations import Min, Max
from agate.columns import Column
from agate.data_types import TypeTester, DataType, Text, Number
from agate.computations import Computation
from agate.mapped_sequence import MappedSequence
from agate.preview import print_table, print_bars
from agate.rows import Row
Expand Down Expand Up @@ -90,7 +89,9 @@ class Table(utils.Patchable):
optional. If specified it may be 1) the name of a single column that
contains a unique identifier for each row, 2) a key function that takes
a :class:`.Row` and returns a unique identifier or 3) a sequence of
unique identifiers of the same length as the sequence of rows.
unique identifiers of the same length as the sequence of rows. The
uniqueness of resulting identifiers is not validated, so be certain
the values you provide are truly unique.
:param _is_fork:
Used internally to skip certain validation steps when data
is propagated from an existing table. When :code:`True`, rows are
Expand Down Expand Up @@ -847,15 +848,20 @@ def join(self, right_table, left_key, right_key=None, inner=False):
return self._fork(rows, column_names, column_types, row_names=row_names)

@classmethod
def merge(cls, tables):
def merge(cls, tables, row_names=None):
"""
Merge an array of tables with identical columns into a single table.
Each table must have exactly the same column types. Their column names
need not be identical. The first table's column names will be the ones
which are used.
Row names will be lost, but new row names can be specified with the
`row_names` argument.
:param tables:
An sequence of :class:`Table` instances.
:param row_names:
See :class:`Table` for the usage of this parameter.
:returns:
A new :class:`Table`.
"""
Expand All @@ -875,7 +881,7 @@ def merge(cls, tables):
for row in table.rows:
rows.append(Row(row.values(), column_names))

return Table(rows, column_names, column_types, row_names=tables[0].row_names, _is_fork=True)
return Table(rows, column_names, column_types, row_names=row_names, _is_fork=True)

@allow_tableset_proxy
def group_by(self, key, key_name=None, key_type=None):
Expand Down
4 changes: 0 additions & 4 deletions tests/test_computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ def test_formula_no_validate(self):
('test', Formula(self.number_type, lambda r: r['one'], validate=False))
])

self.assertIsNot(new_table, self.table)
self.assertEqual(len(new_table.rows), 4)
self.assertEqual(len(new_table.columns), 5)

# Now everything is screwed up
self.assertSequenceEqual(new_table.rows[0], ('a', Decimal('2'), Decimal('3'), Decimal('4'), 'a'))
self.assertEqual(new_table.columns['test'][0], 'a')
Expand Down
13 changes: 10 additions & 3 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,10 +1493,17 @@ def test_merge_different_types(self):

def test_merge_with_row_names(self):
table_a = Table(self.rows, self.column_names, self.column_types, row_names='three')
table_b = Table(self.rows, self.column_names, self.column_types)
table_c = Table.merge([table_a, table_b])

self.assertRowNames(table_c, table_a.row_names)
b_rows = (
(1, 4, 'd'),
(2, 3, 'e'),
(None, 2, 'f')
)

table_b = Table(b_rows, self.column_names, self.column_types, row_names='three')
table_c = Table.merge([table_a, table_b], row_names='three')

self.assertRowNames(table_c, ['a', 'b', 'c', 'd', 'e', 'f'])

class TestData(AgateTestCase):
def setUp(self):
Expand Down

0 comments on commit 6dbb45d

Please sign in to comment.