Home > OS >  How to get the same timestamp from pandas dataframe to json file
How to get the same timestamp from pandas dataframe to json file

Time:09-10

Summary

Need to retain the same timestamp format from pandas dataframe while converting into newline delimited json in python.

Input in dataframe: 2020-08-17 07:38:02
Expected after converting into json: 2020-08-17 07:38:02

My try 1

df.to_json("C:/myfolder/myfile.json",
           orient="records", date_format='iso', date_unit='s',
           lines=True)

Actual result with try 1 : 2020-08-17T07:38:02Z

My try 2

df.to_json("C:/myfolder/myfile.json",
           orient="records",
           lines=True)

Actual result with try 2 : 1597649882000

CodePudding user response:

your type dataframe date column is datetime that's why when u convert it into json it get stocked like this format instead you can convert your column type like this.

df["date"]=df["date"].astype(str)
df.to_json("path",orient="records",lines=True)
  • Related