Skip to content

Instantly share code, notes, and snippets.

@evoshawkins
Last active June 30, 2022 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evoshawkins/1432dc9dd3c6a4358c8eb28688519734 to your computer and use it in GitHub Desktop.
Save evoshawkins/1432dc9dd3c6a4358c8eb28688519734 to your computer and use it in GitHub Desktop.
Error "KeyError: 'id'" on st.dataframe where style.format is used - sensitive to dataframe size
import streamlit as st
import pandas as pd
df = pd.read_csv("https://leg-2-lou-docs-pics.s3.us-west-2.amazonaws.com/reports/invoices_fail_8457_rows.csv", dtype=str, parse_dates=[8,9])
col1, col2 = st.columns(2)
with col1:
st.markdown("# Invoices ")
with col2:
st.metric("Row count", len(df))
st.markdown("## Invoice Data CSV from S3")
#Convert text to float
df['ItemSubTotalAmount'] = df['ItemSubTotalAmount'].astype(float)
df['TaxAmount'] = df['TaxAmount'].astype(float)
df['TotalAmount'] = df['TotalAmount'].astype(float)
# This next line always works
#st.dataframe(df);
# Applying formating fails based on dataframe size
st.dataframe(df.style.format({'ItemSubTotalAmount': '{:.2f}', 'TaxAmount': '{:.2f}', 'TotalAmount': '{:.2f}'}))
import streamlit as st
import pandas as pd
df = pd.read_csv("https://leg-2-lou-docs-pics.s3.us-west-2.amazonaws.com/reports/invoices_success_8456_rows.csv", dtype=str, parse_dates=[8,9])
col1, col2 = st.columns(2)
with col1:
st.markdown("# Invoices ")
with col2:
st.metric("Row count", len(df))
st.markdown("## Invoice Data CSV from S3")
#Convert text to float
df['ItemSubTotalAmount'] = df['ItemSubTotalAmount'].astype(float)
df['TaxAmount'] = df['TaxAmount'].astype(float)
df['TotalAmount'] = df['TotalAmount'].astype(float)
# This next line always works
#st.dataframe(df);
# Applying formating fails based on dataframe size
st.dataframe(df.style.format({'ItemSubTotalAmount': '{:.2f}', 'TaxAmount': '{:.2f}', 'TotalAmount': '{:.2f}'}))
@evoshawkins
Copy link
Author

evoshawkins commented Jun 30, 2022

Run
streamlit run issue.py
or
streamlit run https://gist.github.com/evoshawkins/1432dc9dd3c6a4358c8eb28688519734/raw/c3dbddb868456966cf16222dc9ff3fd5a467e5f8/issue.py

The dataframe will load and format the 3 float columns with 2 trailing decimal places.

On the other hand
streamlit run issue-fail.py
or
streamlit run https://gist.github.com/evoshawkins/1432dc9dd3c6a4358c8eb28688519734/raw/c3dbddb868456966cf16222dc9ff3fd5a467e5f8/issue-fail.py

The dataframe will not load but instead give an error 'KeyError: ['id']'

The different between the two is that the CSV loaded from S3 in the 2nd case has one more row than the one that succeeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment