删除"x天从datetime.timedelta对象开始 [英] Removing " x days " from datetime.timedelta object
本文介绍了删除"x天从datetime.timedelta对象开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个报告,在其中我需要减去两个日期并以的形式返回%H:%M%S
I need to create a report where I need to subtract two dates and return it in the form of %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")
在csv Final_ReportX中,它以以下格式保存:0天00:05:39
in the csv Final_ReportX it saved in the form : 0 days 00:05:39
我需要它仅返回00:05:39,没有几天.
*正则表达式不是一个选择
*regex is not an option
谢谢!
推荐答案
您可以使用将timedelta转换为H:M:S字符串的自定义函数:
you can use a custom function that converts timedelta to H:M:S string:
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
- ref
这篇关于删除"x天从datetime.timedelta对象开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文