Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: transpose support dataset with no headers #560

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,11 @@ def sort(self, col, reverse=False):

return _dset

def transpose(self):
def _transpose_with_headers(self):
"""Transpose a :class:`Dataset`, turning rows into columns and vice
versa, returning a new ``Dataset`` instance. The first row of the
original instance becomes the new header row."""

# Don't transpose if there is no data
if not self:
return

_dset = Dataset()
# The first element of the headers stays in the headers,
# it is our "hinge" on which we rotate the data
Expand All @@ -716,6 +712,35 @@ def transpose(self):
_dset.append(row=row_data)
return _dset

def _transpose_without_headers(self):
"""Transpose a :class:`Dataset`, turning rows into columns and vice
versa, returning a new ``Dataset`` instance. This instance should not
have headers, or the dimension would be invalid."""

_dset = Dataset()

# Add columns as rows in new instance
for index in range(0, len(self._data[0])):
row_data = self.get_col(index)
_dset.append(row=row_data)

return _dset

def transpose(self):
"""Transpose a :class:`Dataset`, turning rows into columns and vice
versa, returning a new ``Dataset`` instance. If the instance has
headers, the first row of the original instance becomes the new header
row."""

# Don't transpose if there is no data
if not self:
return

if self.headers is None:
return self._transpose_without_headers()
else:
return self._transpose_with_headers()

def stack(self, other):
"""Stack two :class:`Dataset` instances together by
joining at the row level, and return new combined
Expand Down
15 changes: 15 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,21 @@ def test_transpose(self):
self.assertEqual(second_row,
("gpa", 90, 67, 50))

def test_transpose_empty_dataset(self):
data = tablib.Dataset()
self.assertEqual(data.transpose(), None)

def test_transpose_with_no_headers(self):
data = tablib.Dataset()
data.append(('Cat', 'Eats fish', 26))
data.append(['Dogs like', '_balls', 48])
data.append([73, 'people', 'sleeps'])
dataTrans = data.transpose()
self.assertEqual(dataTrans[0], ('Cat', 'Dogs like', 73))
self.assertEqual(dataTrans[1], ('Eats fish', '_balls', 'people'))
self.assertEqual(dataTrans[2], (26, 48, 'sleeps'))
self.assertEqual(data.transpose().transpose().dict, data.dict)

def test_transpose_multiple_headers(self):

data = tablib.Dataset()
Expand Down