gpt4 book ai didi

python - 将unix时间戳和可读时间戳的混合列转换为全部unix或全部可读

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:58 25 4
gpt4 key购买 nike

我想用困惑的时间戳来协调我的专栏。将它们全部转为 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']

最佳答案

你用不同的格式解析它两次:

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)

关于python - 将unix时间戳和可读时间戳的混合列转换为全部unix或全部可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57511707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com