Skip to content

Commit

Permalink
Fixes #583 - Allow inserting columns for dataset with headers and no …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
claudep committed Mar 21, 2024
1 parent 4fd9a68 commit 0ce9215
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 3 additions & 8 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,14 @@ def _set_dict(self, pickle):
dict = property(_get_dict, _set_dict)

def _clean_col(self, col):
"""Prepares the given column for insert/append."""
"""Prepares the given column for insert/append. `col` is not supposed to
contain any header value.
"""

col = list(col)

if self.headers:
header = [col.pop(0)]
else:
header = []

if len(col) == 1 and hasattr(col[0], '__call__'):

col = list(map(col[0], self._data))
col = tuple(header + col)

return col

Expand Down
15 changes: 12 additions & 3 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,19 @@ def test_add_column(self):

# With Headers
data.headers = ('fname', 'lname')
new_col = [21, 22]
data.append_col(new_col, header='age')
age_col = [21, 22]
data.append_col(age_col, header='age')
size_col = [1.65, 1.86]
data.insert_col(1, size_col, header='size')

self.assertEqual(data['age'], new_col)
self.assertEqual(data['age'], age_col)
self.assertEqual(data['size'], size_col)

def test_add_column_no_data_with_headers(self):
"""Verify adding empty column when dataset has only headers."""
data.headers = ('fname', 'lname')
data.insert_col(1, [], header='size')
self.assertEqual(data.headers, ['fname', 'size', 'lname'])

def test_add_column_no_data_no_headers(self):
"""Verify adding new column with no headers."""
Expand Down

0 comments on commit 0ce9215

Please sign in to comment.