mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 15:00:19 +00:00
Merge branch 'stacking' into feature/stacking
This commit is contained in:
@@ -547,6 +547,59 @@ class Dataset(object):
|
||||
_dset.append(row=row_data)
|
||||
|
||||
return _dset
|
||||
|
||||
def row_stack(self, other):
|
||||
|
||||
"""Stack two :class:`Dataset` instances together by
|
||||
joining them at the row level, and return a new
|
||||
combined ``Dataset`` instance."""
|
||||
|
||||
if not isinstance(other, Dataset):
|
||||
return
|
||||
|
||||
if self.width != other.width:
|
||||
raise InvalidDimensions
|
||||
|
||||
# Copy the source data
|
||||
_dset = copy(self)
|
||||
|
||||
rows_to_stack = [row for row in _dset._data]
|
||||
other_rows = [row for row in other._data]
|
||||
|
||||
rows_to_stack.extend(other_rows)
|
||||
_dset._data = rows_to_stack
|
||||
|
||||
return _dset
|
||||
|
||||
def column_stack(self, other):
|
||||
|
||||
"""Stack two :class:`Dataset` instances together by
|
||||
joining at the column level, and return a new
|
||||
combined ``Dataset`` instance. Requires headers
|
||||
to be set."""
|
||||
|
||||
if not isinstance(other, Dataset):
|
||||
return
|
||||
|
||||
if not self.headers or not other.headers:
|
||||
raise HeadersNeeded
|
||||
|
||||
if self.height != other.height:
|
||||
raise InvalidDimensions
|
||||
|
||||
new_headers = self.headers + other.headers
|
||||
|
||||
_dset = Dataset()
|
||||
|
||||
for column in self.headers:
|
||||
_dset.append(col=self[column])
|
||||
|
||||
for column in other.headers:
|
||||
_dset.append(col=other[column])
|
||||
|
||||
_dset.headers = new_headers
|
||||
|
||||
return _dset
|
||||
|
||||
def wipe(self):
|
||||
"""Removes all content and headers from the :class:`Dataset` object."""
|
||||
|
||||
@@ -364,7 +364,44 @@ class TablibTestCase(unittest.TestCase):
|
||||
("last_name","Adams", "Washington", "Jefferson"))
|
||||
self.assertEqual(second_row,
|
||||
("gpa",90, 67, 50))
|
||||
|
||||
def test_row_stacking(self):
|
||||
|
||||
"""Row stacking."""
|
||||
|
||||
to_join = tablib.Dataset(headers=self.founders.headers)
|
||||
|
||||
for row in self.founders:
|
||||
to_join.append(row=row)
|
||||
|
||||
row_stacked = self.founders.row_stack(to_join)
|
||||
|
||||
for column in row_stacked.headers:
|
||||
|
||||
original_data = self.founders[column]
|
||||
expected_data = original_data + original_data
|
||||
self.assertEqual(row_stacked[column], expected_data)
|
||||
|
||||
def test_column_stacking(self):
|
||||
|
||||
"""Column stacking"""
|
||||
|
||||
to_join = tablib.Dataset(headers=self.founders.headers)
|
||||
|
||||
for row in self.founders:
|
||||
to_join.append(row=row)
|
||||
|
||||
column_stacked = self.founders.column_stack(to_join)
|
||||
|
||||
for index, row in enumerate(column_stacked):
|
||||
|
||||
original_data = self.founders[index]
|
||||
expected_data = original_data + original_data
|
||||
self.assertEqual(row, expected_data)
|
||||
|
||||
self.assertEqual(column_stacked[0],
|
||||
("John", "Adams", 90, "John", "Adams", 90))
|
||||
|
||||
def test_wipe(self):
|
||||
"""Purge a dataset."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user