ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I am trying to display and interact through row clicks with a streamlit dataframe that takes in a pandas dataframe as an input.
There is sometimes a delay between a row click interaction with the streamlit dataframe and the updating of the event selection object, particularly on the first row click.
Additionally, on row clicks the follow Value Error arises:

Traceback (most recent call last):
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in code_to_exec
    self._session_state.on_script_will_rerun(
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 66, in on_script_will_rerun     
    self._state.on_script_will_rerun(latest_widget_states)
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 514, in on_script_will_rerun
    self._call_callbacks()
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 522, in _call_callbacks
    changed_widget_ids = [
                         ^
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 523, in <listcomp>
    wid for wid in self._new_widget_state if self._widget_changed(wid)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Deployment\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 537, in _widget_changed
    changed: bool = new_value != old_value
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "Deployment\.venv\Lib\site-packages\pandas\core\generic.py", line 1577, in __nonzero__
    raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Also getting dataframe serialisation error:

Serialization of dataframe to Arrow table was unsuccessful due to: ("Expected bytes, got a 'int' object", 'Conversion failed for column Field Value with type object'). Applying automatic fixes for column types to make the dataframe Arrow-compatible.

This is the code snippet:

def download_results(results):
    dfs = []
    if len(results) == 0:
        st.warning("No files found in this batch.")
        return

    for result in results:
        if result and result.get('extracted_fields') is not None:
            df = result['extracted_fields']
            df['filename'] = result['file_name']
            dfs.append(df)
        else:
            logger.error("Error: None returned for extraction.")

    if len(dfs) == 0:
        st.warning("No data extracted from the batch.")
        return

    df_all = pd.concat(dfs)
    df_xlsx = to_excel(df_all)
    
    return df_all    


df_all = download_results(results)
event = st.dataframe(data=df_all, hide_index=True, selection_mode="single-row", on_select="rerun", use_container_width=True, key="bulk-dataframe")
    
    print(event)

When getting this error, I am:

  • Running the app locally
  • Using Python version 3.11.4
  • Using Streamlit version 1.38.0