Skip to content

Tabulate raises ValueError with np.array as cell values #339

Open
@luxedo

Description

Follow up to: pandas-dev/pandas#59588

tabulate can handle lists [1, 2] as cell values but not numpy arrays np.array([1, 2]):

>>> print(tabulate([[[1, 2], 3]]))
------  -
[1, 2]  3
------  -
>>> print(tabulate([[np.array([1, 2]), 3]]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/john/GitHub/os/envs/numpy/lib/python3.12/site-packages/tabulate/__init__.py", line 2048, in tabulate
    list_of_lists, headers = _normalize_tabular_data(
                             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/GitHub/os/envs/numpy/lib/python3.12/site-packages/tabulate/__init__.py", line 1471, in _normalize_tabular_data
    rows = list(map(lambda r: r if _is_separating_line(r) else list(r), rows))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/GitHub/os/envs/numpy/lib/python3.12/site-packages/tabulate/__init__.py", line 1471, in <lambda>
    rows = list(map(lambda r: r if _is_separating_line(r) else list(r), rows))
                                   ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/GitHub/os/envs/numpy/lib/python3.12/site-packages/tabulate/__init__.py", line 107, in _is_separating_line
    (len(row) >= 1 and row[0] == SEPARATING_LINE)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'd expect a similar behavior or at least something like:

----------------  -
np.array([1, 2])  3
----------------  -

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions