将int64列转换为时间列

时间:2020-06-18 12:28:34

标签: python pandas datetime

我有一个带有“时间”列的熊猫数据框,目前看起来像这样:

ab['ZEIT'].unique()
array([     0, 165505, 203355, ...,  73139,  75211,  74244], dtype=int64)

我该如何使用hh:mm:ss从中获取德语时间格式,基本上如此,所以看起来像这样:

array([00:00:00, 16:55:05, 20:33:55, ..., 07:31:39, 07:52:11, 07:42:44], dtype=?)

1 个答案:

答案 0 :(得分:1)

将值转换为带有填充0的字符串后,请使用pd.to_datetime。然后为dt

调用time访问器
In [12]: pd.to_datetime(df['ZEIT'].astype(str).str.zfill(6), format='%H%M%S').dt.time
Out[12]: 
0    00:00:00
1    16:55:05
2    20:33:55
3    07:31:39
4    07:52:11
5    07:42:44
Name: ZEIT, dtype: object

详细信息

In [13]: df
Out[13]: 
     ZEIT
0       0
1  165505
2  203355
3   73139
4   75211
5   74244

In [14]: df['ZEIT'].astype(str).str.zfill(6)
Out[14]: 
0    000000
1    165505
2    203355
3    073139
4    075211
5    074244
Name: ZEIT, dtype: object

In [15]: pd.to_datetime(df['ZEIT'].astype(str).str.zfill(6), format='%H%M%S')
Out[15]: 
0   1900-01-01 00:00:00
1   1900-01-01 16:55:05
2   1900-01-01 20:33:55
3   1900-01-01 07:31:39
4   1900-01-01 07:52:11
5   1900-01-01 07:42:44
Name: ZEIT, dtype: datetime64[ns]