如何格式化日期时间,以便Google表格将值识别为日期时间

问题描述 投票:0回答:1

我目前正在使用gspread将数据从Google表格中检索到pandas DataFrame中。为此,我只是在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"])

它运作良好,但是当我想更新电子表格时出现问题。

如果我仅尝试使用pandas DataFrame更新范围:

sheet.update([df.columns.values.tolist()] + df.values.tolist())

我收到以下错误:

Timestamp类型的对象不可JSON序列化

因此,我相信我需要将时间戳转换回字符串:

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")

更新效果很好,但是数据无法识别为时间戳:

enter image description here

是否有办法让Google Spreadsheet正确识别日期(即正确的格式?)>

我目前正在使用gspread将数据从Google表格中检索到pandas DataFrame中。为此,我只是在https://gspread.readthedocs.io/en/latest/user-guide ....

python dataframe google-sheets-api gspread
1个回答
0
投票

此修改如何?

© www.soinside.com 2019 - 2024. All rights reserved.