Speckle Py Tutorial File Error (KeyError : Index issue)

Speckle users,

I tried the example for the Speckle Python, and I got the Key Error: “index”

The Python example file I am referring is:


st.subheader("Commit Activity Timeline 🕒")
#created at parameter to dataframe with counts
cdate = pd.to_datetime(commits["createdAt"]).dt.date.value_counts().reset_index().sort_values("index")
#date range to fill null dates.
null_days = pd.date_range(start=cdate["index"].min(), end=cdate["index"].max())
#add null days to table
cdate = cdate.set_index("index").reindex(null_days, fill_value=0)
#reset index
cdate = cdate.reset_index()
#rename columns
cdate.columns = ["date", "count"]
#redate indexed dates
cdate["date"] = pd.to_datetime(cdate["date"]).dt.date

The Error I got is:

File “C:\Users\XXXX\Anaconda3\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py”, line 552, in _run_script
exec(code, module.dict)
File “C:\Users\XXXX\SpeckleTutorials\01_specklePystreamActivityApp_tut01.py”, line 250, in
cdate = pd.to_datetime(commits[“createdAt”]).dt.date.value_counts().reset_index().sort_values(“index”)
File “C:\Users\XXXX\Anaconda3\lib\site-packages\pandas\core\frame.py”, line 6766, in sort_values
k = self._get_label_or_level_values(by, axis=axis)
File “C:\Users\XXXX\Anaconda3\lib\site-packages\pandas\core\generic.py”, line 1778, in _get_label_or_level_values
raise KeyError(key)
KeyError: ‘index’

Hey @ashinde_TYL ,

Interesting, i tried to reproduce your error but couldn’t. The error indicated the issue i occurring in the “sort_values” method of the Pandas DataFrame. Can you check the column names in “cdate”?

Should I be using

st.write(cdate.columns) or st.write(commits.columns)? The above image is a screenshot of st.write(commits.columns)

I used this statement. Used sort_index() method

cdate = pd.to_datetime(commits["createdAt"]).dt.date.value_counts().reset_index().sort_index()

and my columns look like this


1 Like

Did this resolve the issue? If it did, please consider marking it as the solution. That way, anyone else who experiences the same issue in the future can benefit from it as well.

It did resolve that issue but now the method for

null_days = pd.date_range(start=cdate["index"].min(), end=cdate["index"].max())

gives the key error

File “C:\Users\abhishek.shinde\Anaconda3\lib\site-packages\pandas\core\indexes\base.py”, line 3654, in get_loc
raise KeyError(key) from err
KeyError: ‘index’

I have the final working solution which i would like to share:

cdate = pd.to_datetime(commits["createdAt"]).dt.date.value_counts().reset_index().sort_index()
null_days = pd.date_range(start= str(cdate.min()[0]), end=str(cdate.max()[0]))
cdate  = cdate.set_index("createdAt").reindex(null_days,fill_value=0)
cdate = cdate.reset_index()

Please let me know @gokermu if this solution works on your end