marktilbrook
marktilbrook

Reputation: 83

Convert a column of unix timestamps to dates

I have a dataframe that looks like this. How would I convert each timestamp to a date? This is only showing the head of the dataframe by the way.

       timestamp    price
0   1615464598245   297.491325
1   1615469525042   300.784423
2   1615471754237   300.918268
3   1615475174397   302.313760
4   1615478420805   304.688945

Upvotes: 1

Views: 187

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

import pandas as pd

Use to_datetime() method:

df['timestamp']=pd.to_datetime(df['timestamp'],unit='ms')

Output:

print(df)

      timestamp                 price
0   2021-03-11 12:09:58.245     297.491325
1   2021-03-11 13:32:05.042     300.784423
2   2021-03-11 14:09:14.237     300.918268
3   2021-03-11 15:06:14.397     202.313760
4   2021-03-11 16:00:20.805     304.688945

Upvotes: 2

Related Questions