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.
alt.Chart(native_data.to_dict('records'))...UserWarning: data of type <class 'list'> not recognized warnings.warn(f"data of type {type(data)} not recognized", stacklevel=1)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)