I have 2 dataframes from a dash datatable, first is the original from the database, second is from the edited datatable.
In this case I have a column which product_date_up
means the last edited timestamp.
I already generate the function to add timestamp every time the user click the save button and the timestamp automatically inserted to the row that was edited.
To do that I am using this code
import sqlalchemy as alch
from datetime import datetime
@app.callback(
Output('table-container-edit','a'),
[Input('edit-button','n_clicks')],
[State('data-table-edit','data'),State('edit-button','click')]
)
def edit_db(n_clicks, dataset, click):
df_table = pd.DataFrame(dataset)
df_original = pd.read_sql(query2,engine)
for row in df_table['product_id']:
if df_table.loc[df_table['product_id']==row, 'product_name'] != df_original.loc[df_original['product_id']==row, 'product_name']:
df_table.loc[df_table['product_id']==row, 'product_date_up'] = datetime.now()
#if name from the edited table is different from database, do insert the timestamp
table_name = 'producttype'
df_table.to_sql(table_name,con=engine, index=False, if_exists='replace',
dtype={
'product_id': alch.CHAR(36),
'product_name': alch.VARCHAR(100),
'product_type': alch.VARCHAR(50),
'product_description': alch.VARCHAR(300),
'product_date_cr': alch.DateTime(),
'product_date_up': alch.DateTime(),
'product_by_cr': alch.VARCHAR(50),
'product_by_up': alch.VARCHAR(50)
})
Seems I can not compare the pandas.Series with !=
and got this error
Traceback (most recent call last):
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\Software\Python 3.6.8\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\Software\Python 3.6.8\lib\site-packages\dash\dash.py", line 1031, in dispatch
response.set_data(func(*args, outputs_list=outputs_list))
File "D:\Software\Python 3.6.8\lib\site-packages\dash\dash.py", line 966, in add_context
output_value = func(*args, **kwargs) # %% callback invoked %%
File "c:\Users\Kenny\Desktop\producttype.py", line 171, in edit_db
if df_table.loc[df_table['product_id']==row, 'product_name'] != df_original.loc[df_original['product_id']==row, 'product_name']:
File "D:\Software\Python 3.6.8\lib\site-packages\pandas\core\generic.py", line 1555, in __nonzero__
self.__class__.__name__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
I have try is not
but it is updating the entire row instead. What can I use?
EDIT
based from the answer from Gingerhaze and little edit, this code works like magic
df_table.loc[(df_original["product_name"] != df_table["product_name"]),"product_date_up"]=datetime.now()