Anna Nikman
Anna Nikman

Reputation: 23

Python timestamp in dataframe - convert into data format

I have dataframe in following format:

    >     buyer_id   purch_id   timestamp  

    >     buyer_2    purch_2    1330767282
    >     buyer_3    purch_3    1330771685
    >     buyer_3    purch_4    1330778269
    >     buyer_4    purch_5    1330780256
    >     buyer_5    purch_6    1330813517

I want to ask for your advice how to convert timestamp column (in dataframe) into datetime and then extract only the time of the event into the new column??

Thanks!

Upvotes: 0

Views: 43

Answers (1)

FObersteiner
FObersteiner

Reputation: 25684

assuming 'timestamp' is Unix time (seconds since the epoch), you can cast to_datetime provided the right unit ('s') and use the time part:

df['time'] = pd.to_datetime(df['timestamp'], unit='s').dt.time

df
Out[9]: 
  buyer_id purch_id   timestamp      time
0  buyer_2  purch_2  1330767282  09:34:42
1  buyer_3  purch_3  1330771685  10:48:05
2  buyer_3  purch_4  1330778269  12:37:49
3  buyer_4  purch_5  1330780256  13:10:56
4  buyer_5  purch_6  1330813517  22:25:17

Upvotes: 1

Related Questions