Skip to content

BUG: to_markdown raises ValueError when values are np.arrays with more than one element #59588

Closed
@luxedo

Description

@luxedo

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

>>> s = pd.Series([np.array([1, 2])])
>>> s
0    [1, 2]
dtype: object 
>>> s.to_markdown()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/site-packages/pandas/core/series.py", line 1970, in to_markdown
  return self.to_frame().to_markdown(
File "/home/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/site-packages/pandas/util/_decorators.py", line 333, in wrapper
  return func(*args, **kwargs)
File "/home/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/site-packages/pandas/core/frame.py", line 2984, in to_markdown
  result = tabulate.tabulate(self, **kwargs)
File "/home/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/site-packages/tabulate/__init__.py", line 2048, in tabulate
  list_of_lists, headers = _normalize_tabular_data(
File "/home/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/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/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/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/amu4ca/repo/venvs/sentinela-studies-main/lib/python3.10/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() 

Issue Description

When trying to convert a Series or a DataFrame to markdown with np.ndarrays as values an error is raised.

Expected Behavior

The method should print something similar to lists behavior:

>>> pd.Series([[1, 2]]).to_markdown()  # All Good!
'|    | 0      |\n|---:|:-------|\n|  0 | [1, 2] |' 

Perhaps any iterable should be converted to a list before sending to tabulate.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.146.1-microsoft-standard-WSL2
Version : #1 SMP Thu Jan 11 04:09:03 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.2
numpy : 2.0.1
tabulate : 0.9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions