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!