Hey @Charly_Wargnier,
The issue doesn’t seem to be related to @st.cache
.
DataFrame apply()
is not called as intended.
dfToken.apply()
takes a function as first argument (a function which takes a column or a row as parameter), but here you’re performing a function call with TokenClassScores()
which returns a modified dfToken
.
So what you’re doing is roughly equivalent to something like this:
dfToken['ratioClass'] = np.where(dfToken['Box #01'] == dfToken['Box #02'], 'Duplicate', 'Low score')
dfToken.apply(dfToken, axis=1) # dfToken is a DataFrame, it should've been a function/lambda instead.
The issue is not @st.cache
.
I was able to reproduce your issue without @st.cache
. Note that in my following code samples, I’ll use dfToken.apply(...)
as you did, even though it shouldn’t be used like this.
Display code
import numpy as np
import pandas as pd
import streamlit as st
dfToken = pd.DataFrame({
"ratioClass": [None, None, None, None, None],
"Box #01": ["A", "A", "B", "A", "B"],
"Box #02": ["A", "B", "A", "A", "B"]
})
"Initial DataFrame"
dfToken
"DataFrame apply"
dfToken.apply(dfToken, axis=1)
dfToken
The weird thing is, if I add the dfToken['ratioClass']
line right before dfToken.apply(...)
, this doesn’t seem to trigger any error. I’d need to dig into pandas’s internal functioning to explain that specific behavior. Here’s the result:
Display code
"DataFrame apply"
dfToken['ratioClass'] = np.where(dfToken['Box #01'] == dfToken['Box #02'], 'Duplicate', 'Low score')
dfToken.apply(dfToken, axis=1)
dfToken

However, if I remove that dfToken.apply(...)
, it still works as expected, without weird behavior occuring:
Display code
"DataFrame apply"
dfToken['ratioClass'] = np.where(dfToken['Box #01'] == dfToken['Box #02'], 'Duplicate', 'Low score')
dfToken

What should you do?
It depends on the workflow of your application.
As shown above, you don’t need dfToken.apply(...)
to change a whole column. Only one dfToken['ratioClass'] = np.where(...)
does the trick. But I don’t have the whole picture of that TokenClassScores()
, so I can’t really tell what you could cache.
I hope it helped!