Cannot Display Styled Dataframe with NaN values

I have added a colormap to a dataframe using matplotlibs colormaps.

df.style.background_gradient(axis=None, cmap='coolwarm')

This works fine for columns with only NaN values but throws an error when a column in the dataframe has both numbers and NaN values.

The error is thrown when trying to display the styled dataframe with “st.dataframe()”

ValueError: cannot convert to ‘float64’-dtype NumPy array with missing values. Specify an appropriate ‘na_value’ for this dtype.

File "C:\Work\Systems\Maturity Assessment\engineering-maturity\Maturity.py", line 90, in create_project_rollup
    st.dataframe(heat_mapped_deviation_table, use_container_width=True, hide_index=True, height=(len(latest_assessments) + 1) * 35 + 3)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\streamlit\runtime\metrics_util.py", line 410, in wrapped_func
    result = non_optional_func(*args, **kwargs)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\streamlit\elements\arrow.py", line 546, in dataframe
    marshall_styler(proto, data, default_uuid)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\streamlit\elements\lib\pandas_styler_utils.py", line 61, in marshall_styler
    styler._compute()
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\io\formats\style_render.py", line 258, in _compute
    r = func(self)(*args, **kwargs)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\io\formats\style.py", line 1719, in _apply
    result = func(data, **kwargs)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\io\formats\style.py", line 3926, in _background_gradient
    gmap = data.to_numpy(dtype=float)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\core\frame.py", line 1843, in to_numpy
    result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\core\internals\managers.py", line 1770, in as_array
    arr = self._interleave(dtype=dtype, na_value=na_value)
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\core\internals\managers.py", line 1829, in _interleave
    arr = blk.values.to_numpy(  # type: ignore[union-attr]
File "C:\Work\Tools\Anaconda3\envs\Streamlit\lib\site-packages\pandas\core\arrays\masked.py", line 412, in to_numpy
    raise ValueError(

As you can see, Streamlit is calling to Numpy to convert the dataframe to a numpy array with “data.to_numpy(dtype=float)” which cannot seem to handle columns with both numbers and NaN values.

Any ideas on how to fix this? I can’t really replace the NaNs with 0 as 0 has a different meaning in my data.

They do handle NaN values, my guess is that data in your dataframe are not numeric. What are the df.dtypes of your dataframe?

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.