Python Pandas read_excel and to_json date format error

不羁岁月 提交于 2021-01-27 11:38:39

问题


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                           

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.

Below is the code snippet;

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

Thanks


回答1:


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

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

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"}]'


来源:https://stackoverflow.com/questions/34920285/python-pandas-read-excel-and-to-json-date-format-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!