I have a dataframe with a column of dates expressed as timestamps (like [1554334020000, 1554334200000, 1554334380000, 1554334560000, 1554334740000]
). How do I convert it into datetime?
I know that for a single value you can do
datetime(1970, 1, 1) + timedelta(milliseconds=int(data['Date'].values[0]))
but how do I apply that to the whole column?
Use pandas.to_datetime
passing argument unit='ms'
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df = pd.DataFrame({'timestamp': [1554334020000, 1554334200000, 1554334380000,
1554334560000, 1554334740000]})
print(df)
[out]
timestamp
0 1554334020000
1 1554334200000
2 1554334380000
3 1554334560000
4 1554334740000
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
print(df)
[out]
timestamp
0 2019-04-03 23:27:00
1 2019-04-03 23:30:00
2 2019-04-03 23:33:00
3 2019-04-03 23:36:00
4 2019-04-03 23:39:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With