【问题标题】:Use `pandas.to_timedelta` with decimal periods使用带有小数点的`pandas.to_timedelta`
【发布时间】:2021-10-21 03:12:39
【问题描述】:

在我的 CSV 中,我有一列持续时间写为 13:08.413:06.20

我想将此列转换为 pandas timedelta。

但是,当我尝试 df['Time'] = pd.to_timedelta(df['Time']) 时,我收到错误 ValueError: expected hh:mm:ss format before .

发生了什么,我该如何解决?

【问题讨论】:

标签: python pandas timedelta time-format


【解决方案1】:

您可以将'.' 替换为':' 就像错误所说的以hh:mm:ss 的格式:

df['Time'] = pd.to_timedelta(df['Time'].str.replace('.',':'))
#13:08.4 becomes: 0 days 13:08:04

如果“时间”的格式是mm:ss,则使用:

df['Time'] = pd.to_timedelta('00:'+df['Time'])
#13:08.4 becomes: 0 days 00:13:08.400000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多