Python Pandas read_excel 和 to_json 日期格式错误

Python Pandas read_excel and to_json date format error

本文关键字:日期 json 格式 错误 to Pandas read excel Python      更新时间:2023-09-26

下面是来自excel的数据,我正在尝试使用pandas read_excelto_json函数将其转换为JSON。JSON 日期的字段"Date"1449446400000(不带引号)。我想知道为什么日期显示为大数字而不是12/7/2015.

ID    Date      Name   Lat        Long     Pick Success Failure Drop Amount
===========================================================================
5   12/7/2015   PSG 11.0231335  77.0016396  31    21      10    44   5192                           

请让我知道如何在 JSON 中将其转换为正确的日期格式,以便我可以用来生成一些 JavaScript 图表。

下面是代码片段;

def home(request):
    df = pandas.read_excel('<here goes the excel path>')
    json = df.to_json(orient="records")
    return render(request, 'home.html', {'data':json})

谢谢

写入

json 时,必须使用 以下命令设置date_format

json = df.to_json(orient="records", date_format='iso')

由于默认值为"epoch",如果不将其显式设置为"iso",因此您将以纪元毫秒为单位获得结果。这将返回示例输出:

'[{"id":5,"date":"2015-07-12T00:00:00.000Z"}]'