将时间戳列表字符串转换为熊猫中的时间戳列表

时间:2021-05-25 07:07:25

标签: python pandas

我从 s3 解析数据,这与此类似

ID   departure
1    "[Timestamp('2021-05-25 09:00:00'), datetime.datetime(2021, 5, 25, 9, 21, 35, 769406)]"
2    "[Timestamp('2021-05-25 08:00:00'), datetime.datetime(2021, 5, 25, 11, 15), datetime.datetime(2021, 5, 25, 14, 15)]"

有什么办法可以把departure转成list

我试过了

samp['departure'] = samp['departure'].apply(lambda x: eval(x))
-> Error: eval() arg 1 must be a string, bytes or code object

samp['departure'] = samp['departure'].apply(lambda x: x[1:-1].split(','))
# Here datetime.datetime(2021, 5, 25, 11, 15) splited into many sub-parts

samp.departure = samp.departure.apply(ast.literal_eval)
error -> malformed node or string: ["Timestamp('2021-05-25 09:00:00')", ' datetime.datetime(2021', ' 5', ' 25', ' 9', ' 21', ' 35', ' 769406)']

输出应该是

ID   departure
1    [Timestamp('2021-05-25 09:00:00'), datetime.datetime(2021, 5, 25, 9, 21, 35, 769406)]
2    [Timestamp('2021-05-25 08:00:00'), datetime.datetime(2021, 5, 25, 11, 15), datetime.datetime(2021, 5, 25, 14, 15)]

(我最初在 read_csv 时尝试了转换器,但也出现错误)

1 个答案:

答案 0 :(得分:1)

如果您尝试替换离开列中的 "

通过 replace() 尝试:

samp['departure']=samp['departure'].replace('"','',regex=True)

通过 strip() 尝试:

samp['departure']=samp['departure'].str.strip('"')

如果您正在评估里面的值:

from pandas import Timestamp
import datetime
samp['departure']=samp['departure'].astype(str).apply(pd.eval)

from pandas import Timestamp
import datetime
import ast
samp.departure = samp.departure.astype(str).apply(ast.literal_eval)