[英]Pandas: Derive a days, hours, minutes, seconds timestamp from available data
With this expected data, I am trying to see if I can return the following result:有了这个预期的数据,我想看看我是否可以返回以下结果:
#expected result
'12.22:05:38.3911208'
# mwe
df = pd.DataFrame({
'wrong_ts': '1970-01-13T22:05:38.391+0000',
'minutes': 18605,
'seconds': 38,
'ms': 391,
'expected': '12.22:05:38.3911208'})
This gets me fairly close, but not quite:这让我非常接近,但不完全是:
df['duration'] = df['minutes'].astype(str) + 'm ' + df['seconds'].astype(str) + 's ' + df['ms'].astype(str) + 'ms'
pd.to_timedelta(df.duration)
# 12 days 22:05:38.391000
You could try this.你可以试试这个。 Using
to_pytimedelta
and converting the time to string.使用
to_pytimedelta
并将时间转换为字符串。 Then from the resulting string, split and get the days and time in the required format.然后从生成的字符串中拆分并获取所需格式的日期和时间。
time_delta = pd.to_timedelta(df.duration)
my_date = time_delta.dt.to_pytimedelta().astype(str)
split_time = my_date[0].split(',')
days = split_time[0]
time = split_time[1].strip()
print(days.split('days')[0].strip() + '.' + time)
Output:输出:
12.22:05:38.391000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.