![](/img/trans.png)
[英]Pandas: Derive a days, hours, minutes, seconds timestamp from available data
[英]Converting Pandas column from days to days, hours, minutes
我正在尝试将仅包含十进制天数的列df["time_ro_reply"]
转换为包含天、小时、分钟的 timedelta 格式。 这使它更具人类可读性。
我正在阅读有关 pd.to_timedelta 的信息,但我正在努力实现它: pd.to_timedelta(df["time_to_reply"])
这只返回 0。
样本输入:
df["time_ro_reply"]
1.881551
0.903264
2.931560
2.931560
预期 output:
df["time_ro_reply"]
1 days 19 hours 4 minutes
0 days 23 hours 2 minutes
2 days 2 hours 23 minutes
2 days 2 hours 23 minutes
我建议使用自定义 function 如下:
import numpy as np
import pandas as pd
# creating the provided dataframe
df = pd.DataFrame([1.881551, 0.903264, 2.931560, 2.931560],
columns = ["time_ro_reply"])
# this function converts a time as a decimal of days into the desired format
def convert_time(time):
# calculate the days and remaining time
days, remaining = divmod(time, 1)
# calculate the hours and remaining time
hours, remaining = divmod(remaining * 24, 1)
# calculate the minutes
minutes = divmod(remaining * 60, 1)[0]
# a list of the strings, rounding the time values
strings = [str(round(days)), 'days',
str(round(hours)), 'hours',
str(round(minutes)), 'minutes']
# return the strings concatenated to a single string
return ' '.join(strings)
# add a new column to the dataframe by applying the function
# to all values of the column 'time_ro_reply' using .apply()
df["desired_output"] = df["time_ro_reply"].apply(lambda t: convert_time(t))
这会产生以下 dataframe:
time_ro_reply desired_output
0 1.881551 1 days 21 hours 9 minutes
1 0.903264 0 days 21 hours 40 minutes
2 2.931560 2 days 22 hours 21 minutes
3 2.931560 2 days 22 hours 21 minutes
但是,这会产生与您描述的不同的输出。 如果 'time_ro_reply' 值确实被解释为纯小数,我看不出你是如何得到预期结果的。 你介意分享你是如何得到它们的吗?
我希望评论能很好地解释代码。 如果不是并且您不熟悉 divmod()、apply() 等语法,我建议您在 Python / Pandas 文档中查找它们。
让我知道这是否有帮助。
使用 MrB here所示的漂亮 function 的修改版本,
def display_time(seconds, granularity=2):
intervals = (('days', 86400),
('hours', 3600),
('minutes', 60),
('seconds', 1),
('microseconds', 1e-6))
result = []
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
name = name.rstrip('s') if value == 1 else name
result.append(f"{int(value)} {name}")
else:
result.append(f"{0} {name}")
return ', '.join(result[:granularity])
如果您将“time_to_reply”列转换为秒并应用 function,您还可以获得所需的 output:
import pandas as pd
df = pd.DataFrame({"time_to_reply": [1.881551, 0.903264, 2.931560, 2.931560]})
df['td_str'] = df['time_to_reply'].apply(lambda t: display_time(t*24*60*60, 3))
# df['td_str']
# 0 1 day, 21 hours, 9 minutes
# 1 0 days, 21 hours, 40 minutes
# 2 2 days, 22 hours, 21 minutes
# 3 2 days, 22 hours, 21 minutes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.