【问题标题】:How to display pydatatable frame output in streamlit web app?如何在 streamlit Web 应用程序中显示 pydatatable 帧输出?
【发布时间】:2021-10-27 21:08:30
【问题描述】:

我有一个脚本可以在 streamlit 应用程序中将数据表帧输出显示为:

import datatable as dt
import streamlit as st
import pandas as pd

st.set_page_config(
page_title="pydatatable demo",
layout="wide",
initial_sidebar_state="expanded")

DT = dt.Frame({
    'class':['a','b','c','d','e'],
    'score':[1,2,3,4,5]
})

st.table(DT)

在执行这个脚本时,它会发出一个错误:

2021-08-28 05:34:40.905 Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/streamlit/script_runner.py", line 350, in _run_script
    exec(code, module.__dict__)
  File "/content/pydt_demo.py", line 16, in <module>
    st.table(DT)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/elements/dataframe_selector.py", line 118, in table
    return self.dg._arrow_table(data)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/elements/arrow.py", line 119, in _arrow_table
    marshall(proto, data, default_uuid)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/elements/arrow.py", line 160, in marshall
    proto.data = type_util.data_frame_to_bytes(df)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/type_util.py", line 371, in data_frame_to_bytes
    table = pa.Table.from_pandas(df)
  File "pyarrow/table.pxi", line 1479, in pyarrow.lib.Table.from_pandas
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 591, in dataframe_to_arrays
    for c, f in zip(columns_to_convert, convert_fields)]
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 591, in <listcomp>
    for c, f in zip(columns_to_convert, convert_fields)]
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 577, in convert_column
    raise e
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 571, in convert_column
    result = pa.array(col, type=type_, from_pandas=True, safe=safe)
  File "pyarrow/array.pxi", line 301, in pyarrow.lib.array
  File "pyarrow/array.pxi", line 83, in pyarrow.lib._ndarray_to_array
  File "pyarrow/error.pxi", line 84, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: ('Could not convert    | class\n   | str32\n-- + -----\n 0 | a    \n 1 | b    \n 2 | c    \n 3 | d    \n 4 | e    \n[5 rows x 1 column]\n with type datatable.Frame: did not recognize Python value type when inferring an Arrow data type', 'Conversion failed for column 0 with type object')

作为一种解决方法,如果我将此数据表框架转换为熊猫框架,它会完美显示输出。

【问题讨论】:

    标签: python streamlit py-datatable


    【解决方案1】:

    streamlit.table() 似乎不支持数据表框架:https://docs.streamlit.io/en/stable/api.html#streamlit.table

    因此,目前唯一的选择是将您的框架转换为任何受支持的框架,即pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, Iterable, dict, or None。幸运的是,datatable 支持转换为所有这些格式:https://datatable.readthedocs.io/en/latest/api/frame.html#convert-into-other-formats

    【讨论】:

    • 是的,我现在尝试转换
    猜你喜欢
    • 2021-08-30
    • 2020-05-25
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2012-01-02
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多