将unix时间戳和可读时间戳的混合列转换为所有unix或所有可读

时间:2019-08-15 15:02:00

标签: python pandas unix-timestamp

我想使一列与混乱的时间戳记保持一致。将它们全部转换为unix或可读。请告诉我是否有简便的方法。

te[13090:13110]
13090                    1561571794
13091                    1561571957
13092                    1561572119
13093                    1561572280
13094                    1561572442
13095                    1561572606
13096                    1561572767
13097                    1561572931
13098                    1561573095
13099                    1561573258
13100                    1561573419
13101    2019-06-26 18:27:44.000000
13102    2019-06-26 18:30:36.000000
13103    2019-06-26 18:33:27.000000
13104    2019-06-26 18:36:15.000000
13105    2019-06-26 18:39:05.000000
13106    2019-06-26 18:41:52.000000
13107    2019-06-26 18:44:37.000000
13108    2019-06-26 18:47:26.000000
13109    2019-06-26 18:50:26.000000
Name: timestamp, dtype: object

如果您想重现问题的列表

['1561571794', '1561571957', '1561572119', '1561572280', '1561572442', '1561572606', '1561572767', '1561572931', '1561573095', '1561573258', '1561573419', '2019-06-26 18:27:44.000000', '2019-06-26 18:30:36.000000', '2019-06-26 18:33:27.000000', '2019-06-26 18:36:15.000000', '2019-06-26 18:39:05.000000', '2019-06-26 18:41:52.000000', '2019-06-26 18:44:37.000000', '2019-06-26 18:47:26.000000', '2019-06-26 18:50:26.000000']

1 个答案:

答案 0 :(得分:0)

您使用不同的格式对其进行了两次解析:

df = pd.DataFrame({
    'DateTimeStr': ['1561571794', '1561571957', '1561572119', '1561572280', '1561572442', '1561572606', '1561572767', '1561572931', '1561573095', '1561573258', '1561573419', '2019-06-26 18:27:44.000000', '2019-06-26 18:30:36.000000', '2019-06-26 18:33:27.000000', '2019-06-26 18:36:15.000000', '2019-06-26 18:39:05.000000', '2019-06-26 18:41:52.000000', '2019-06-26 18:44:37.000000', '2019-06-26 18:47:26.000000', '2019-06-26 18:50:26.000000']
})

d1 = pd.to_datetime(df['DataTimeStr'], format='%Y-%m-%d %H:%M:%S.%f', errors='coerce')
idx = d1[d1.isna()].index
d2 = pd.to_datetime(df.loc[idx, 'TimeStr'].astype('int'), unit='s')

df['DateTime'] = d1.combine_first(d2)