0

I have a data frame which one of the column is in epoch time:

df1['timestamp'] 

is something like this:

1533009600000
1533013200000  
1533016800000 



timestamp    object

I need to convert to human readable time stamp. I tried this:

import time
df1['timestamp']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(df1['timestamp']))

I get this error:

TypeError: cannot convert the series to <type 'float'>

what I am doing wrong here?

user1471980
  • 10,127
  • 48
  • 136
  • 235
  • @Prune, I am following the datetime conversion, but I am getting some weird error. It is really not the same type of question. – user1471980 Aug 17 '18 at 18:57
  • My apologies; thanks for the extra focus. The problem is in the Pandas handling. The original column is of type `float`, and you're changing that on the fly. You need the proper astype() conversion to do this. – Prune Aug 17 '18 at 18:59
  • what is the unit of the timestamp? – ALollz Aug 17 '18 at 19:01

1 Answers1

4

You can convert based on the unit of the epoch time. The number is too large for s, assuming it is ms

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


    timestamp
0   2018-07-31 04:00:00
1   2018-07-31 05:00:00
2   2018-07-31 06:00:00
Vaishali
  • 37,545
  • 5
  • 58
  • 86