diff options
| author | Kenneth Reitz <me@kennethreitz.com> | 2010-11-17 19:56:04 -0500 |
|---|---|---|
| committer | Kenneth Reitz <me@kennethreitz.com> | 2010-11-17 19:56:04 -0500 |
| commit | d992ece86a4953da218b6d991132ca496a973d20 (patch) | |
| tree | 6e5665e11b6163f432a4b784c925d571edbbfbba /tablib | |
| parent | 46f302255dc4b807114eaca3c9584323f1913f81 (diff) | |
| parent | 36bbe2726bb014188241164956273096ac795c05 (diff) | |
| download | tablib-d992ece86a4953da218b6d991132ca496a973d20.tar.gz | |
Merge branch 'stacking' into feature/stacking
Diffstat (limited to 'tablib')
| -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.""" |
