Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feature request: support passing DataFrames to table.table #28726

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

Closed
dawnwangcx opened this issue Aug 15, 2024 · 4 comments · Fixed by #28830
Closed

feature request: support passing DataFrames to table.table #28726

dawnwangcx opened this issue Aug 15, 2024 · 4 comments · Fixed by #28830

Comments

@dawnwangcx
Copy link

Bug summary

When trying to input a dataframe to a table, it will raise keyerror. Since cellText [0] will not give the first row of the dataframe and thus not returning the correct column length. So, I made a modification to recognize cellText as dataframe.
contact: [email protected]

Code for reproduction

df = pd.DataFrame() #any dataframe
table = table.table(ax,df,loc='center')

Actual outcome

Traceback (most recent call last):
File "C:\Users\053232\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3653, in get_loc
return self._engine.get_loc(casted_key)
File "pandas_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "c:/Users/Downloads/suspension_check.py", line 147, in
generate_image(suspended_etfs,'suspended_etfs')
File "c:/Users/Downloads/suspension_check.py", line 106, in generate_image
tbl = table.table(ax, df, loc='center')
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\table.py", line 754, in table
cols = len(cellText[0])
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 3761, in getitem
indexer = self.columns.get_loc(key)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3655, in get_loc
raise KeyError(key) from err
KeyError: 0

Expected outcome

return a table contains a dataframe

Additional information

Check we have some cellText

if cellText is None:
    # assume just colours are needed
    rows = len(cellColours)
    cols = len(cellColours[0])
    cellText = [[''] * cols] * rows
elif isinstance(cellText, pd.DataFrame):
    # 获取DataFrame的列名
    column_names = cellText.columns.values
    cellText = np.vstack([column_names, cellText.values])

Operating system

No response

Matplotlib Version

3.7.5

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

pip

@tacaswell
Copy link
Member

I would not consider this a bug a the input is documented as a 2D list of strings (https://matplotlib.org/stable/api/table_api.html#matplotlib.table.table) .

That said, this may be a reasonable feature request as I don't think it would be too hard to detect if we were passed a pd.DataFrame and then convert it to a list-of-list-of-str at the top of table.table.

@tacaswell tacaswell added this to the future releases milestone Aug 15, 2024
@tacaswell tacaswell changed the title [Bug]: feature request: support passing DataFrames to table.table Aug 15, 2024
@timhoffm
Copy link
Member

@dawnwangcx You are welcome to submit you proposal as a pull request.

@StefRe
Copy link
Contributor

StefRe commented Oct 6, 2024

This is what pandas.plotting.table was made for. Is it necessary to reinvent the wheel at the possible expense of an additional dependency here?

@timhoffm
Copy link
Member

timhoffm commented Oct 6, 2024

I wasn't aware of this. Nevertheless, I think it's nice to support this natively in matplotlib. Not any priority, but if somebody wants to contribute this, I'm fine with it:

  • It saves the user from having to know about pandas.plotting.table
  • The user does not need to import a standalone pandas function
  • It's really just a handful of lines to implement
  • With the discussed pattern, we don't add a dependency.

That said, we should align our implementation with the pandas one.

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

Successfully merging a pull request may close this issue.

6 participants