[英]Removing " x days " from datetime.timedelta object
I need to create a report where I need to subtract two dates and return it in the form of %H:%M%S我需要创建一个报告,我需要减去两个日期并以 %H:%M%S 的形式返回它
This is the subtraction which I inserted into a list :这是我插入列表的减法:
time_difference_temp=datetime.strptime(next_time,"%Y-%m-%d %H:%M:%S") - datetime.strptime(current_time,"%Y-%m-%d %H:%M:%S")
time_difference.append(time_difference_temp)
return time_difference
this list returns to >此列表返回到 >
def push_to_csv(time_difference):
df = pd.read_csv('time_differences.csv')
df["Delta"] = time_difference
df.dropna(subset=["Data"], inplace=True)
df.to_csv("Final_ReportX.csv")
in the csv Final_ReportX it saved in the form : 0 days 00:05:39在 csv Final_ReportX 中,它以以下形式保存:0 days 00:05:39
I need it to return just 00:05:39, no days.我需要它在 00:05:39 返回,没有几天。
*regex is not an option *正则表达式不是一个选项
Thanks!谢谢!
you can use a custom function that converts timedelta to H:M:S string:您可以使用自定义函数将 timedelta 转换为 H:M:S 字符串:
def td_to_str(td):
"""
convert a timedelta object td to a string in HH:MM:SS format.
"""
hours, remainder = divmod(td.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
return f'{int(hours):02}:{int(minutes):02}:{int(seconds):02}'
s = pd.Series(pd.to_timedelta(['1 day, 00:05:39']))
s.apply(td_to_str)
# 0 24:05:39
# dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.