.to_timedelta 的问题

Problems with .to_timedelta

我正在尝试按照 中的步骤通过转换“pandas._libs.tslibs.timestamps.Timestamp”系列向我的数据帧添加时间增量列。

代码:

df = pd.DataFrame([['2020-07-25 09:26:28',2],['2020-07-25 09:26:2',10],['2020-07-25 09:26:30',203],['2020-07-25 09:26:31',30]], 
                      columns = ['Time','Load'])

df['Time'] = pd.to_datetime(df['Time'])
print(df)
print(type(df["Time"][0]))
df_static['time_delta'] = pd.to_timedelta(df_static['Time'])

However, I get the following error:

TypeError: dtype datetime64[ns] 无法转换为 timedelta64[ns]

What am I doing wrong?

如果需要时间从日期时间转换为 timedeltas 将日期时间转换为格式 HH:MM:SS by Series.dt.strftime:

df['td'] = pd.to_timedelta(pd.to_datetime(df['Time']).dt.strftime('%H:%M:%S'))