Reputation: 37
I wanted to convert the UTC timestamp with milliseconds in dataframe table to local time stamp with only hh:mm:ss format.This is what I have done so far.
Code
import pandas as pd
import requests
import json
from datetime import date
from datetime import datetime
import pytz
val_list = [['VAL_1', [[1587319860000, 62.468963623046875], [1587319920000, 62.46857198079427], [1587319980000, 62.46759033203125]]], ['VAL_2', [[1587319860000, 65.64366149902344], [1587319920000, 65.64424133300781], [1587319980000, 65.64410909016927]]], ['VAL_3', [[1587319860000, 72.03440348307292], [1587319920000, 72.03465779622395], [1587319980000, 72.03514099121094]]]]
index = [x[0] for x in val_list[0][1]]
val_dict = dict(val_list)
df = pd.DataFrame(val_dict, index=index)
for col in df.columns:
df[col] = [elem[1] for elem in df[col]]
print(df)
OutPut
VAL_1 VAL_2 VAL_3
1587319860000 62.468964 65.643661 72.034403
1587319920000 62.468572 65.644241 72.034658
1587319980000 62.467590 65.644109 72.035141
Code
df.reset_index(level=0, inplace=True) #converting index to column name index
df.rename(columns={'index': 'TimeStamp'}, inplace=True) #renaming column name
df['TimeStamp'] = (pd.to_datetime(df['TimeStamp'], unit='ms')
.dt.tz_localize('utc')
.dt.tz_convert('Asia/Calcutta')) #Timezone
print(df)
Output
TimeStamp VAL_1 VAL_2 VAL_3
0 2020-04-19 23:41:00+05:30 62.468964 65.643661 72.034403
1 2020-04-19 23:42:00+05:30 62.468572 65.644241 72.034658
2 2020-04-19 23:43:00+05:30 62.467590 65.644109 72.035141
I wanted to remove 2020-04-19 and +05:30 and only keep 23:41:00. Any suggestions will be helpful.
Upvotes: 0
Views: 583
Reputation: 3770
You can try this:
[23]: df['time']=df['TimeStamp'].apply(lambda d: d.time())
In [24]: df
Out[24]:
TimeStamp VAL_1 VAL_2 VAL_3 time
0 2020-04-19 23:41:00+05:30 62.468964 65.643661 72.034403 23:41:00
1 2020-04-19 23:42:00+05:30 62.468572 65.644241 72.034658 23:42:00
2 2020-04-19 23:43:00+05:30 62.467590 65.644109 72.035141 23:43:00
d.time() basically extracts the time component from the timestamp
Upvotes: 1