【问题标题】:how do I convert hh:mm:ss (0:1:19) to minutes using pandas如何使用 pandas 将 hh:mm:ss (0:1:19) 转换为分钟
【发布时间】:2021-05-05 22:14:32
【问题描述】:

我想将以下数据转换为分钟,但我总是出错

time = 0:1:19

time1 = df['time']

time2 = time1.hour * 60 + time1.minute + time1.second

我收到此错误:

 "AttributeError: 'Series' object has no attribute 'hour'"

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

使用pd.to_timedelta 将时间转换为timedelta 以获得秒数。

time1 = pd.Series(['0:1:19', '1:01:19', '0:0:19'])
obj = pd.to_timedelta(time1)
# convert to minutes
obj.dt.seconds / 60.0

    0     1.316667
    1    61.316667
    2     0.316667
    dtype: float64

【讨论】:

    猜你喜欢
    • 2013-07-30
    • 2014-09-18
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多