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

Fixes #583 - Allow inserting columns for dataset with headers and no values #584

Merged
merged 1 commit into from
Mar 22, 2024
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
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
Loading