[Python] Altair fails to render chart out of pandas dataframe on Streamlit

Discussão em 'Python' iniciado por Stack, Julho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code in Python, using Streamlit as framework:

    try:
    native_data = data.copy()

    # Create Altair chart with native data
    st.write(f"Debug: Native data type: {type(native_data)}")
    chart = alt.Chart(native_data).mark_bar().encode(
    x=alt.X(x_col, type='nominal'),
    y=alt.Y(y_col, type='quantitative')
    ).properties(
    title=self._label,
    width=400,
    height=300
    )

    except Exception as chart_creation_error:
    st.write(f"Debug: Chart creation error: {chart_creation_error}")
    return "table_only"

    try:
    container.altair_chart(chart, use_container_width=True)
    return "success"

    except Exception as render_error:
    st.write(f"Debug: Chart rendering error: {render_error}")
    st.write(f"Debug: Render error type: {type(render_error)}")
    import traceback
    st.write("Debug: Render error traceback:")
    st.code(traceback.format_exc())
    raise render_error


    And this is the debug output:

    Debug: Native data type: <class 'pandas.core.frame.DataFrame'>

    Debug: Chart rendering error: You passed a <class 'narwhals.stable.v1.DataFrame'> to is_pandas_dataframe.


    I can see that the dataframe used to generate the Altair object is a Pandas dataframe but, nevertheless, Altair seems to confuse it with a Narwhals dataframe.

    I haven't found a single reference to a similar case, so any help will be much appreciated.

    Continue reading...

Compartilhe esta Página