使用熊猫绘制日期时间与值

时间:2021-05-17 16:16:19

标签: python pandas dataframe datetime

我需要绘制三列“日期”、“时间”和“值”。

df = pd.read_excel(r'C:\Users\markl\OneDrive\Dokument\Downloads\file.xlsx') 


df['DateTime'] = pd.to_datetime(df.pop('Date')) + pd.to_timedelta(df.pop('Time')) 
#Combine columns "Date" and "Time"

df['DateTime'] = df['DateTime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M:%S.%f"))

这会产生值错误:时间数据“2021-02-22 12:31:59”与格式“%Y-%m-%d %H:%M:%S.%f”不匹配

>

这是为什么??

1 个答案:

答案 0 :(得分:0)

我相信格式字符串末尾的“.%f”意味着它期望在秒数之后出现某种微秒数,而您的时间数据似乎在秒数之后停止。我做了以下工作,它有效:

import datetime

string = "2021-02-22 12:31:59"
datetime.datetime.strptime(string, '%Y-%m-%d %H:%M:%S')