You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

呼叫中心数据处理:pd.to_timedelta报format参数错误的问题咨询

解决pd.to_timedelta()的format参数报错及计算平均通话时长

错误原因

你使用的pandas版本不支持pd.to_timedelta()的format参数——这个参数是在pandas 1.1.0及之后的版本才新增的,旧版本调用时会触发TypeError。

解决办法

方法1:升级pandas版本

如果环境允许,直接升级到支持format参数的版本:

pip install --upgrade pandas

升级后,你的原代码就能正常运行:

df['Interaction_Duration'] = pd.to_timedelta(df['Interaction_Duration'], format='%M:%S')

方法2:不升级版本,手动转换格式

如果无法升级pandas,可通过两种方式手动处理字符串:

方式A:转成总秒数再转换

# 将"MM:SS"字符串转为总秒数
df['total_seconds'] = df['Interaction_Duration'].apply(lambda x: int(x.split(':')[0])*60 + int(x.split(':')[1]))
# 转换为timedelta类型
df['Interaction_Duration'] = pd.to_timedelta(df['total_seconds'], unit='s')

方式B:替换字符串为pandas可识别的时长格式

# 把"MM:SS"替换为"MM minutes SS seconds"格式,让to_timedelta自动解析
df['Interaction_Duration'] = pd.to_timedelta(df['Interaction_Duration'].str.replace(':', ' minutes '))

计算平均通话时长

转换完成后,直接调用mean()方法即可得到平均时长:

avg_call_duration = df['Interaction_Duration'].mean()
# 若需要转换为分钟格式输出
print(f"平均通话时长:{avg_call_duration.total_seconds()/60:.2f} 分钟")

内容的提问来源于stack exchange,提问作者Mostafa Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 12:48:25