I'm currently using gspread to retrieve data from a Google sheet into a pandas DataFrame. In order to do that, I simply followed their example at https://gspread.readthedocs.io/en/latest/user-guide.html#using-gspread-with-pandas
df = pd.DataFrame(sheet.get_all_records())
df["From"] = pd.to_datetime(df["From"])
df["To"] = pd.to_datetime(df["To"])
It works well but problems arise when I want to update the spreadsheet.
If I only try to update the range with the pandas DataFrame:
sheet.update([df.columns.values.tolist()] + df.values.tolist())
I get the following error:
Object of type Timestamp is not JSON serializable
Therefore, I believe I need to cast the timestamp back to a string:
df["From"] = df["From"].dt.strftime("%Y-%m-%d %H:%M:%S.%f")
df["To"] = df["To"].dt.strftime("%Y-%m-%d %H:%M:%S.%f")
The update works well, but the data are not recognised back as timestamps:
Is there a way to get Google Spreadsheet to recognise the date correctly (i.e. what would be the correct format)?
%f
portion? – Dianoetic%f
portion. The answer from @Tanaike actually does the trick. – Decembrist