python - 如何将 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
谢谢
毫米
解决方案
根据数据分析,我可以得出结论,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)
推荐阅读
- c# - MySQL C# 连接错误,无法连接
- python - 我试图掩盖熊猫的日期,之前和之后
- kendo-grid - Kendo Grid 导出选中的行
- javascript - jQuery fadeIn() 反复调用不起作用
- javascript - Discord.JS TypeError:尝试添加角色时无法读取未定义的属性“角色”
- c# - YamlDotNet:将对象反序列化为字符串,每个对象之间带有行分隔符
- spring - Spring Security + CAS + Tomcat:不监听url:/login/cas
- smtp - 可以多次发出 EHLO 或 HELO SMTP 命令吗?
- python - CGI 代码不工作:内部服务器 500
- reactjs - 在不同版本的 StrictMode 中不推荐使用 findDOMNode