在pandas
0 1602206700206666
1 1602206700206668
2 1602206700206670
3 1602206700206672
4 1602206700206674
我正在尝试将其转换为日期时间。下面是我使用的代码:
df['time'] = pd.to_datetime(df['time'], unit="ns", utc=True)
然而,我得到了一个奇怪的输出
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
很奇怪,因为日期不应该是1970-01,而应该是2020-10。有什么办法解决这个问题吗?
它们是unix时间戳,从epoch开始以毫秒为单位。Pandas对整数使用纳秒。所以尝试:
pd.to_datetime(df['time']*1000)
输出:
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]
您的时间以微秒为单位,因此使用us
代替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