Df.style.applymap gives error since a few days

EDIT: It has to do with the upgrade to the new pandas 1.3.0 from 1.1.4 what I used before. Apparently there is now a Styler object and I guess my code is not backwards compatible :frowning:

EDIT 2: it has been solved, hopefully it will be soon active at share.streamlit.io DataFrame Styling not working when using pandas 1.3.0 · Issue #3526 · streamlit/streamlit · GitHub

Hello

This code gives an error since a few days. Locally everything is allright and before there were no problems

Expected output

image

The error

Traceback (most recent call last): File “/app/covidcases/covid_menu_streamlit.py”, line 63, in main module.main()

File “/app/covidcases/show_contactmatrix.py”, line 66,

in main st.write (df_first_pivot.style.format(None, na_rep="-").applymap(lambda x: cell_background_helper(x,“lineair”, 10, None)).set_precision(2))

File “/home/appuser/venv/lib/python3.7/site-packages/streamlit/elements/write.py”, line 181, in write self.dg.dataframe(arg)

File “/home/appuser/venv/lib/python3.7/site-packages/streamlit/elements/data_frame.py”, line 85, in dataframe marshall_data_frame(data, data_frame_proto)

File “/home/appuser/venv/lib/python3.7/site-packages/streamlit/elements/data_frame.py”, line 150, in marshall_data_frame _marshall_styles(proto_df.style, df, styler)

File “/home/appuser/venv/lib/python3.7/site-packages/streamlit/elements/data_frame.py”, line 169, in _marshall_styles translated_style = styler._translate()

TypeError: _translate() missing 2 required positional arguments: ‘sparse_index’ and ‘sparse_cols’

The code

import streamlit as st
import pandas as pd

def cell_background_test(val,max):
    """Creates the CSS code for a cell with a certain value to create a heatmap effect
    Args:
        val ([int]): the value of the cell

    Returns:
        [string]: the css code for the cell
    """
    opacity = 0
    try:
        v = abs(val)
        color = '193, 57, 43'
        value_table = [ [0,0],
                        [0.25,0.25],
                        [0.50,0.50],
                        [0.75,0.75],
                        [1,1]]
        for vt in value_table:
            if v >= round(vt[0]*max) :
                opacity = vt[1]
    except:
        # give cells with eg. text or dates a white background
        color = '255,255,255'
        opacity = 1
    return f'background: rgba({color}, {opacity})'

def  make_legenda(max_value):
        stapfracties =   [0, 0.0625 , 0.125,  0.25,  0.50, 0.75,  1]
        stapjes =[]
        for i in range(len(stapfracties)):
            stapjes.append((stapfracties[i]*max_value))
        d = {'legenda': stapjes}

        df_legenda = pd.DataFrame(data=d)
        st.write (df_legenda.style.format(None, na_rep="-").applymap(lambda x:  cell_background_test(x,max_value)).set_precision(2))

def main():
    st.header("This is just a sandbox")
    make_legenda(150)
    st.stop()

if __name__ == "__main__":
    main()

Hi, is there a way around for this? I have a list of columns ending with ‘_CAUTION’, and locally it is not showing any problem but streamlit throws the Styler error. Is there any chance we can bypass without using df.style.applymap to highlighting certain or max values?

It looks like you’ve encountered an issue with Pandas Styler when upgrading to Pandas 1.3.0, causing errors in Streamlit. The error message suggests that there’s an issue with the _translate method in the Styler object.

As a workaround, you can try using a previous version of Pandas, such as 1.1.4, which you mentioned worked fine before the upgrade to 1.3.0. You can specify the Pandas version in your requirements.txt file like this:

pandas==1.1.4

This should revert your Pandas version to the one that worked without any issues in Streamlit. Keep in mind that using an older version means you might miss out on new features and improvements in the latest Pandas release, so consider this workaround as a temporary solution until the Styler issue is resolved.

Additionally, you can monitor the GitHub issue you mentioned (Issue #3526 on Streamlit’s GitHub) for updates and solutions related to this problem. The Streamlit community and developers are actively working on addressing such issues.

The way around the issue in the OP is upgrading to a recent version of Streamlit, as explained in the edits. Anythng less than two years old will do.