Python Pandas read_excel和to_json日期格式错误 [英] Python Pandas read_excel and to_json date format error

查看:67
本文介绍了Python Pandas read_excel和to_json日期格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is the data from an excel which I am trying to convert to JSON using pandas read_excel and to_json functions. The JSON date has the field "Date" as 1449446400000 (without quotes). I am wondering why the date is displayed as a big number instead of 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图表.

Please let me know how to convert it into a proper date format in JSON so that I can use to to generate some JavaScript charts.

下面是代码段;

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 :

You have to set the date_format when writing to json with:

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

由于默认值为'epoch',而未将其显式设置为'iso',因此您可以以epoch毫秒为单位获取结果.这将返回一个示例输出:

Since the default is 'epoch', without setting it explicity to 'iso', you're getting your results in epoch milliseconds. This returns for a sample output:

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

这篇关于Python Pandas read_excel和to_json日期格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆