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

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

Closed
3 tasks done
luxedo opened this issue Aug 23, 2024 · 4 comments
Closed
3 tasks done
Labels
Bug Closing Candidate May be closeable, needs more eyeballs IO Data IO issues that don't fit into a more specific label

Comments

@luxedo
Copy link

luxedo commented Aug 23, 2024

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

@luxedo luxedo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 23, 2024
@asishm
Copy link
Contributor

asishm commented Aug 23, 2024

thanks for the report. pandas directly calls tabulate for markdown conversion. I'd try raising an issue there (but the library seems to be unmaintained).

@rhshadrach
Copy link
Member

rhshadrach commented Aug 25, 2024

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

-1 here, this would break valid use-cases. E.g.

s = pd.Series([{1, 2}])
print(s.to_markdown())
# |    | 0      |
# |---:|:-------|
# |  0 | {1, 2} |

It seems to me if tabulate does not support the values in the cell, the user should be responsible for converting them. Trying to do anything else opens up many issues for various use-cases.

@rhshadrach rhshadrach added IO Data IO issues that don't fit into a more specific label Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2024
@mroeschke
Copy link
Member

Seems like something pandas shouldn't work around so closing

@luxedo
Copy link
Author

luxedo commented Sep 2, 2024

thanks for the report. pandas directly calls tabulate for markdown conversion. I'd try raising an issue there (but the library seems to be unmaintained).

Yes, this also makes me question if this dependency is even necessary given that pandas probably is not using many of it's features. Couldn't this be the case to remove this dependency and implement a simple converter?

I opened an issue at tabulate astanin/python-tabulate#339

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

No branches or pull requests

4 participants