首页 > 解决方案 > 如何将 Azure 备份报告持续时间列转换为带十进制数字的日期时间

问题描述

Azure 云备份报告中的 Duration 列似乎有一个有趣的时间格式(小时后和分钟后的十进制)。您能否帮助解决小时、分钟和秒部分,以使该列以小时为单位?

1.05:27:39.9470724
21:17.7
21:41.4
1.02:42:37.1136811
21:17.2

我尝试格式化微秒部分,但不确定如何以小时为单位解决这些小数。很高兴排除这些小数。

appended_data['Duration'] = pd.to_datetime(appended_data['Duration'], format='%H:%M:%S.%f')
ValueError: time data '1.05:27:39.9470724' does not match format '%H:%M:%S.%f' (match)
appended_data['Backup Size'] = appended_data['Backup Size'].str.replace('MB','')
appended_data['DurationFixed'] = pd.to_timedelta(df['Duration'].str.split(':',expand=True)\
                    .stack()\
                    .astype(float)\
                    .round()\
                    .astype(int).astype(str).unstack(1).fillna('00').agg(':'.join,axis=1),
               unit='s')
appended_data['DurationHours'] = appended_data['DurationFixed'] / np.timedelta64(1,'h')



appended_data['Duration']
1    04:01:22.7756139
1    03:31:17.0678262
1    04:41:32.7253765
1    03:11:18.3396588
1    04:51:20.2017034
           ...       
1    02:21:17.8554095
1    02:21:19.5547075
1    03:41:23.8876812
1    02:21:32.5529160
1    02:01:20.3247238


appended_data['DurationFixed']
1   02:01:20
1   02:01:20
1   02:01:20
1   02:01:20
1   02:01:20
      ...   
1   02:01:20
1   02:01:20
1   02:01:20
1   02:01:20
1   02:01:20

谢谢

毫米

标签: pythonpandasazuredatetime

解决方案


根据数据分析,我可以得出结论,hh部分的小数位实际上是天。示例 2.4:30:30 = 2 天 4 小时 30 分钟 30 秒。

def cleanhours(x):
    hms=x.split(":")
    dh=hms[0].split(".")
    if len(dh)>1:
        hms[0]=str(int(dh[-1])+24*int(dh[-2]))
    hms[2] = hms[2].split(".")[0]
    return int(hms[0])+int(hms[1])/60.0+int(hms[2])/3600.0
#     return ":".join(hms)

推荐阅读