python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

Tabulate raises `ValueError` with `np.array` as cell values

Open luxedo opened this issue 5 months ago • 0 comments

Follow up to: https://github.com/pandas-dev/pandas/issues/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
----------------  -

luxedo avatar Sep 02 '24 02:09 luxedo