String 熊猫-罐头盒';t将字符串强制转换为日期时间

String 熊猫-罐头盒';t将字符串强制转换为日期时间,string,pandas,dataframe,lambda,strftime,String,Pandas,Dataframe,Lambda,Strftime,我有一个datframe,它存储了文本文件中的一些信息,这些信息提供了有关执行作业的详细信息 我将所有这些信息存储在一个名为“df_tmp”的数据帧中。在该数据帧上,我有一个列“end_Date”,我想在其中存储文件的最后一行的结束日期,但如果在该数据帧中我没有任何值,我想存储当前_时间 假设我的文件中的信息位于以下变量上: string_from_my_file = 'Execution time at 2019/10/14 08:06:44' 我需要的是: 如果我的手动文件在最后一行没有任

我有一个datframe,它存储了文本文件中的一些信息,这些信息提供了有关执行作业的详细信息

我将所有这些信息存储在一个名为“df_tmp”的数据帧中。在该数据帧上,我有一个列“end_Date”,我想在其中存储文件的最后一行的结束日期,但如果在该数据帧中我没有任何值,我想存储当前_时间

假设我的文件中的信息位于以下变量上:

string_from_my_file = 'Execution time at 2019/10/14 08:06:44'
我需要的是:

如果我的手动文件在最后一行没有任何日期,我想存储当前时间。 为此,我尝试使用以下代码:

now = dt.datetime.now()
current_time = now.strftime('%H:%M:%S')
df_tmp['end_date'] = df_tmp['end_date'].fillna(current_time).apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S') if not pd.isnull(x) else pd.to_datetime(re.search("([0-9]{4}\/[0-9]{2}\/[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2})", str(df_tmp['string_from_my_file']))[0]))  
但是,它给了我以下错误:

builtins.AttributeError: 'str' object has no attribute 'strftime'
我做错了什么

谢谢

试试这个:

df_tmp['end_date'] = df_tmp['end_date'].fillna(current_time).apply(lambda x: pd.to_datetime(x).strftime('%Y-%m-%d %H:%M:%S') if not pd.isnull(x) else pd.to_datetime(re.search("([0-9]{4}\/[0-9]{2}\/[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2})", str(df_tmp['string_from_my_file']))[0])) 
在此部分中,
lambda x:pd.to_datetime(x).strftime(“%Y-%m-%d%H:%m:%S”
,需要将
x
更改为datetime才能应用
strftime()

您的错误的可能原因: 即使
end\u date
列的类型为datetime,但您正在使用
str
类型的值填充该列。这将更改
end\u date
列的数据类型