0

enter image description here I want to convert the timestamp into the readable date format column. But when i tried the following code, the output of date is all the same. Can anyone help me with this problem?

import json
import pandas as pd
with open('/Users/Damon/Desktop/percent-utx-os-in-profit.json', 'r') as f:
    data = json.load(f)
df = pd.DataFrame(data)

——> what df looks like before

from datetime import date
df["date"] = pd.to_datetime(df.t)

——> what you get and what you want to get

  • This is definitely a duplicate. You can do `df["date"] = pd.to_datetime(df["date"])` & format the result as needed. Check this link https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas – moys Dec 19 '19 at 04:28
  • I tried as what you said. But as i said, the dates returned back are all the same. I want to know what is reason for it. i have attached a pic – CHOINCE1001 Dec 19 '19 at 04:44
  • try `pd.to_datetime(df['t'], unit='s')` you will have to change `ms` to relevant unit based on the resolution of your time that has been recorded – moys Dec 19 '19 at 04:53
  • Appreciate for your helpful answer. Can you also tell me about how i can identify the unit i should use? In other words, when to use "ms" vs "s" for unit? – CHOINCE1001 Dec 19 '19 at 05:26
  • It depends on the unit that was used to record the data in the first place, so, i do not know. You may be able to tell with the number of digits, but i am not completely sure. However, did this work for you? – moys Dec 19 '19 at 05:40
  • yes, it worked for me when i add unit='s' ^^ – CHOINCE1001 Dec 19 '19 at 07:09
  • Does this answer your question? [How to change the datetime format in pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – Ivo Merchiers Dec 19 '19 at 07:23
  • @CHOINCE1001 This question is pretty similar to other. Now that your question has been answered, you can delete the question or i am put my answer & you can accept it. – moys Dec 19 '19 at 07:37

0 Answers0