如何将 unix 纪元转换为高度准确的时间戳 [pandas]



我有一个带有 unix 纪元的时间序列,我想将其转换为格式为"%Y-%m-%d %H:%M:%S.%f"的时间。

当我使用

   pd.to_datetime(data['Time'],format='%Y-%m-%d %H:%M:%S.%f')
我得到的日期大约是 1970-01-01 00:23

:03.270,这是不正确的(因为我的数据实时时间在 2013-11-01 左右(当我尝试时

   pd.to_datetime(data['Time'],unit='ms')

我有正确的日期,但我没有纳米精度。另一方面,当我结合单位和格式时,例如

   pd.to_datetime(data['Time'],unit='ms',format='%Y-%m-%d %H:%M:%S.%f')

我收到一个代码错误,说"无法同时指定格式和单位"。

你知道如何解决这样的问题吗?

一些

示例数据作为名为df的数据帧:

            Time
0   1.383260e+12
1   1.383260e+12
2   1.383260e+12
3   1.383260e+12
4   1.383260e+12
5   1.383260e+12
6   1.383260e+12
7   1.383260e+12
8   1.383260e+12
9   1.383260e+12
10  1.383340e+12
11  1.383340e+12
12  1.383340e+12
13  1.383340e+12
14  1.383340e+12
15  1.383340e+12
16  1.383340e+12
17  1.383340e+12
18  1.383340e+12
19  1.383340e+12

转换为datetime对象,然后设置所需的格式:

pd.to_datetime(df.Time, unit='ms').dt.strftime('%Y-%m-%d %H:%M:%S.%f')

这给出了:

0     2013-10-31 22:53:20.000000
1     2013-10-31 22:53:20.000000
2     2013-10-31 22:53:20.000000
3     2013-10-31 22:53:20.000000
4     2013-10-31 22:53:20.000000
5     2013-10-31 22:53:20.000000
6     2013-10-31 22:53:20.000000
7     2013-10-31 22:53:20.000000
8     2013-10-31 22:53:20.000000
9     2013-10-31 22:53:20.000000
10    2013-11-01 21:06:40.000000
11    2013-11-01 21:06:40.000000
12    2013-11-01 21:06:40.000000
13    2013-11-01 21:06:40.000000
14    2013-11-01 21:06:40.000000
15    2013-11-01 21:06:40.000000
16    2013-11-01 21:06:40.000000
17    2013-11-01 21:06:40.000000
18    2013-11-01 21:06:40.000000
19    2013-11-01 21:06:40.000000
尝试与

astype结合使用?

pd.to_datetime(data['time'].astype(float)/1e9, unit='ms')

最新更新