1

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.

4
  • The only thing that I found is github. But also I think, you can pass data to Altair in the form of a dictionary. (Not correctly - but works) alt.Chart(native_data.to_dict('records'))... Commented Jul 26 at 19:17
  • @Sindik it doesn't work, I get this error: UserWarning: data of type <class 'list'> not recognized warnings.warn(f"data of type {type(data)} not recognized", stacklevel=1) Commented Jul 26 at 19:49
  • @HuLu Vica try to convert your data in advance into the altair.Data class - it accepts a list of dictionaries as input. You can try. Here is such code. new_data = alt.Data(values=native_data.to_dict()) chart = alt.Chart(new_data) Commented Jul 27 at 6:52
  • @HuLuViCa can you make a fully reproducible example please? From the same discussion on github, it looks like this is easy to fix, it just needs a reproducer. The code you've pasted isn't even runnable Commented Jul 28 at 15:43

1 Answer 1

0

Looks like this is a bug in Altair==5.4.0

import streamlit as st
import altair as alt
import pandas as pd

native_data = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
x_col = 'a'
y_col = 'b'

# 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='foo',
   width=400,
   height=300
)
   
st.container().altair_chart(chart, use_container_width=True)

TypeError: You passed a `<class 'narwhals.stable.v1.DataFrame'>` to `is_pandas_dataframe`.

Hint: Instead of e.g. `is_pandas_dataframe(df)`, did you mean `is_pandas_dataframe(df.to_native())`?

If you upgrade your Altair version to at least 5.5.0, then it should pass

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.