7

whenever i convert dataFrame to_json any date format changes to "epoch time"

[{"idNo":1234567891012,"Name":"Jack","genderType":"male","Date":1544572800000,"branchName":"NY","location":"loc1","pCode":123}] Original date was Date:2018-12-12

my python code

@app.route("/fileviewer/" , methods=["GET" , "POST"]) def fileviewer(name): dest = (file_destination) df = pd.read_excel(dest) print(df) x1=df.to_json(orient ='records') print(x1) render_template('fileviewer.html',x=df.to_html()) return render_template('fileviewer.html',x=x1) 

df prints fine

x1 prints with the "epoch time"

2 Answers 2

6

You can change the format using the option date_format:

>>> df = pandas.DataFrame([ {'A' : 0, 't' : datetime.datetime(2018, 1, 2)}, {'A' : 1, 't' : datetime.datetime(2018, 1, 5)}, {'A' : 2, 't' : datetime.datetime(2018, 1, 7)} ]) >>> df.to_json(date_format = 'iso') '{"A":{"0":0,"1":1,"2":2},"t":{"0":"2018-01-02T00:00:00.000Z","1":"2018-01-05T00:00:00.000Z","2":"2018-01-07T00:00:00.000Z"}}' 
Sign up to request clarification or add additional context in comments.

8 Comments

so in my case df.to_json(orient ='records',date_format = 'iso')?
it worked but it add time to it :S , i don't need to add a time stamp here.2018-12-12T00:00:00.000Z
is it possible to get just a plain date without the timestamp?
@SujayDSa How do you mean?
@caverac when do a to_json it converts 2018-01-05 to 2018-01-05T00:00:00.000Z. So I am forced to convert my date to a string separately and then do a to_json
|
0

I found a great solution here

df.assign( **df.select_dtypes(['datetime']).astype(str).to_dict('list') ).to_json(orient="records") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.