Dcc.store is stripping my datetime index out of the dictionary

Hey folks,

Thanks to Adam Schroeder I have found my way into Dash recently, loving it, and I have come across a little stumbling block.

I have put together a stripped down replica of my project to recreate the scenario and will provide a brief explanation.

Basically I have an upload button on a home page. Once a csv file is uploaded, it is processed and stored in the session using dcc.store.

on clicking view analytics button a new page is loaded and the stored data is used to create a data frame

When loading the csv file the index is set to a datetime index. Its a bank balance sheet, Date, Details,Amount. Very simple.

storing the uploaded data changes it. I have included a test_lab,.py that demonstrates converting the df to dict using ‘series’ and recreating df.from_dict works perfectly.

However with dcc.store in between that step the dates get removed and the new data frame then as integer indexing instead.

I have been wrecking my brains for a couple of days searching high and low for a solution. I have narrowed it down to the dcc.store mechanism and am at a loss as to the solution.

in the attached project run index.py and if you clck on analytics button without uploading a file it will load the default dataset corrects, to show it works, if you upload the provided dataset and click view analytics the date is replaced with nuerical index.

run test_lab.py and look at data frame from series output for what it should be like.

the outputs are printed in the console


Zip file

Thank You!

Hello @techienomad,

Welcome to the community!

I normally do df.to_dic(‘records’) when sending data to dcc.Stores or tables in the layout. Have you tried this?

To convert it back, all you have to do is df = pd.DataFrame(data) where data is the dcc.Store.

If you are having an issue and the index for the dataframe really is the time, then when you send it, you need to do df.reset_index().to_dict(‘records’) this will put the time into its own column, index.

Hey, thank you for your help, so that has made some progress however now what is oriignall a dat is coming up as a date and a time, when put into a new data frame it has an index then the date then details and amount. I would like the date itself to be the index:

so using your method to reset index and create dict the dict is as such:

{'Date': Timestamp('2022-11-29 00:00:00'), 'Details': 'John Spartan', 'Amount': -45.9}, {'Date': Timestamp('2022-11-29 00:00:00'), 'Details': 'Simon Pheonix', 'Amount': -11.68},

that is then stored into dcc.store

on another page i simply print out the stored dict and this is how it now looks:

{'Date': '2022-11-29T00:00:00', 'Details': 'John Spartan', 'Amount': -45.9}, {'Date': '2022-11-29T00:00:00', 'Details': 'Simon Pheonix', 'Amount': -11.68},

i then create a data frame using pd.DataFrame()

and this is the output:

 <bound method NDFrame.head of                    Date         Details   Amount
0   2022-11-29T00:00:00    John Spartan   -45.90
1   2022-11-29T00:00:00   Simon Pheonix   -11.68
2   2022-11-29T00:00:00     Mason Storm    17.19

which I need to look like this:

default data frame: <bound method NDFrame.head of                    Details   Amount
2022-11-29    John Spartan   -45.90
2022-11-29   Simon Pheonix   -11.68
2022-11-29     Mason Storm    17.19

Nearly there!

Ok I solved it with these two lines of code after reading the data from dcc.store:

data_frame = pd.DataFrame(data)
data_frame['Date'] = data_frame['Date'].apply(pd.to_datetime, dayfirst=True)
data_frame.set_index('Date', inplace=True)

Thank you for your help :))

1 Like

Yup. You just needed to make sure the data was in datetime and set the index to it. :grin:

1 Like