-3

Currently i am using for loop which takes a lot of time :

for i in range (0,len(df)):
    df['time1[i]']=(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(df.time[i])))

Can someone suggest a way to do it without a loop and faster?

Rakesh
  • 81,458
  • 17
  • 76
  • 113

1 Answers1

1

You can simply use pd.to_datetime with unit = 'ms' if you have other unit of time like seconds, minute, you can assign in unit section.

time
1527910027791
1527911500516

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

2018-06-02 03:27:07.791
2018-06-02 03:51:40.516

And if your timestamp is in UTC, then you can simply convert to your localtime

df['time'] = pd.to_datetime(df['time'], unit= 'ms').dt.tz_localize('utc') \
    .dt.tz_convert(tz='USE YOUR TIME ZONE HERE').apply \
    (lambda x: datetime.replace(x, tzinfo=None))

if needed info like GMT + some hours, then you can ignore the code that use apply and lambda from above

user96564
  • 1,578
  • 5
  • 24
  • 42