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

Inserting column with insert_col fails if dataset contains 0 rows #583

Closed
LukasK1 opened this issue Mar 19, 2024 · 3 comments
Closed

Inserting column with insert_col fails if dataset contains 0 rows #583

LukasK1 opened this issue Mar 19, 2024 · 3 comments

Comments

@LukasK1
Copy link

LukasK1 commented Mar 19, 2024

I am trying to add additional column to a Dataset that is created from a dictionary. I have headers defined separately. However, when dictionary is empty (no rows to export because how data is filtered) insert_col method fails with error:

File "/usr/local/lib/python3.10/site-packages/tablib/core.py", line 372, in _clean_col
    header = [col.pop(0)]
IndexError: pop from empty list

After looking in to the code of the method _clean_col, my guess, that if statement:

tablib/src/tablib/core.py

Lines 373 to 376 in 4fd9a68

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

should be opposite:

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

or:

        if self.headers:
            header = []
        else:
            header = [col.pop(0)]
@LukasK1
Copy link
Author

LukasK1 commented Mar 19, 2024

To reproduce:

>>> from tablib import Dataset
>>> ds = Dataset()
>>> ds.headers = ["First", "Second", "Third"]
>>> ds.insert_col(1, [], header="Insert")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/tablib/core.py", line 544, in insert_col
    col = self._clean_col(col)
  File "/usr/local/lib/python3.10/site-packages/tablib/core.py", line 372, in _clean_col
    header = [col.pop(0)]
IndexError: pop from empty list

claudep added a commit to claudep/tablib that referenced this issue Mar 21, 2024
@claudep
Copy link
Contributor

claudep commented Mar 21, 2024

Thanks for the report. See the patch I suggested to fix this issue. Please tell me what you think.

@LukasK1
Copy link
Author

LukasK1 commented Mar 22, 2024

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants