Home > Enterprise >  converting epoch time to time zone in pandas
converting epoch time to time zone in pandas

Time:10-23

I have a following dataframe with following epoch time as columns in pandas

0    1602206700206666
1    1602206700206668
2    1602206700206670
3    1602206700206672
4    1602206700206674

I am trying to convert this to datetime. The following is the code I used:

df['time'] = pd.to_datetime(df['time'], unit="ns", utc=True)

However, I get a strange output of

0   1970-01-19 13:03:26.700206666 00:00
1   1970-01-19 13:03:26.700206668 00:00
2   1970-01-19 13:03:26.700206670 00:00
3   1970-01-19 13:03:26.700206672 00:00
4   1970-01-19 13:03:26.700206674 00:00

which is strange because the date should not be 1970-01 but should be 2020-10. Any ideas how to fix this?

CodePudding user response:

They are unix timestamp, which is in milliseconds since epoch. Pandas uses nanosecond on integers. So try:

pd.to_datetime(df['time']*1000)

Output:

0   2020-10-09 01:25:00.206666
1   2020-10-09 01:25:00.206668
2   2020-10-09 01:25:00.206670
3   2020-10-09 01:25:00.206672
4   2020-10-09 01:25:00.206674
Name: time, dtype: datetime64[ns]

CodePudding user response:

Your time is in microseconds, so use us instead of ns:

df['time'] = pd.to_datetime(df['time'], unit="us", utc=True)

print (df)

                              time
0 2020-10-09 01:25:00.206666 00:00
1 2020-10-09 01:25:00.206668 00:00
2 2020-10-09 01:25:00.206670 00:00
3 2020-10-09 01:25:00.206672 00:00
4 2020-10-09 01:25:00.206674 00:00
  • Related