diff options
Diffstat (limited to 'tablib/core.py')
| -rw-r--r-- | tablib/core.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tablib/core.py b/tablib/core.py index 816e0e1..4f86b41 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -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.""" |
