简体   繁体   English

从时间戳列中添加和减去秒列 pandas

[英]Adding and subtracting seconds column from timestamp column pandas

import pandas as pd
import numpy as np 
example=[["11/19/20","9:40:28","9:40:00:0","00:00:00.2","101"],
     ["12/22/20","9:29:28","9:29:28:15", "00:10:28.0","102"],
     ["2/17/21","9:20:20","9:20:20:2","0:00:05.2","206"]]

example_table= pd.DataFrame(example,columns=["Date","Start_Time","timestamp","Seconds","ID"])

desired_info=[["11/19/20","9:40:28","9:40:00:0","00:00:00.2","101", "9:40:00:2"],
     ["12/22/20","9:29:28","9:29:28:15", "00:10:28.0","102", "9:40:56:15"],
     ["2/17/21","9:20:20","9:20:20:2","0:00:05.2","206","9:20:25:4"]]

desired_table= pd.DataFrame(desired_info,columns=["Date","Start_Time","timestamp","Seconds", "CID","Finish_Time"])

# I can convert one of my time columns 
example_table.Seconds=example_table.Seconds.apply(pd.to_timedelta)
example_table.Seconds=example_table.Seconds.dt.total_seconds()
example_table['Start_Time']=pd.to_datetime(example_table['Start_Time'], format= '%H:%M:%S').dt.time

Ultimately, I want to be able to add the seconds column which contains miliseconds to the timestamp column.最终,我希望能够将包含毫秒的秒列添加到时间戳列。

When I try the following:当我尝试以下操作时:

example_table["Finish"]=example_table['timestamp']+example_table['Seconds']

# I get the error: 
# can only concatenate str (not "float") to str

Since I'm getting desperate, so I think, maybe I can settle for using the Start_Time in the calculation instead.因为我越来越绝望,所以我想,也许我可以在计算中使用 Start_Time。

# if I try with the Start_Time column:
["Finish"]=example_table['Start_Time']+example_table['Seconds']

# unsupported operand type(s) for +: 'datetime.time' and 'float'

So Next I try to convert the timestamp column using different strategies.所以接下来我尝试使用不同的策略转换时间戳列。 ```# when I try to convert the timestamp column, I get many different errors depending on the strategy ```# 当我尝试转换时间戳列时,根据策略我会得到许多不同的错误

pd.to_timedelta(example_table.timestamp, unit='ms')
#Error: unit must not be specified if the input contains a str
pd.to_timedelta(example_table.timestamp)
#Error: expected hh:mm:ss format```

Eventually, I will use the Finish Time, which is really my offset time in another experiment to find other information as shown here, Find a subset of columns based on another dataframe?最后,我将使用完成时间,这实际上是我在另一个实验中的偏移时间来查找其他信息,如此处所示, 根据另一个 dataframe 查找列的子集?

first you need to create a proper datetime object.首先你需要创建一个合适的日期时间 object。

df = example_table

df['desired_date'] = pd.to_datetime(df['Date'] + ' ' 
                     + df['timestamp'],format='%m/%d/%y %H:%M:%S:%f')

then convert the Seconds column into a timesdelta and add it to the desired date.然后将Seconds列转换为 timesdelta 并将其添加到所需的日期。

we'll have to add some formatting to get your target string format.我们必须添加一些格式来获得您的目标字符串格式。

df['desired_date'] =  (
 df['desired_date'] 
 + 
 pd.to_timedelta(df['Seconds'])
 ).dt.strftime('%H:%M:%S:%f').str.rstrip('0')


print(df)

       Date Start_Time   timestamp     Seconds   ID desired_date
0  11/19/20    9:40:28   9:40:00:0  00:00:00.2  101   09:40:00:2
1  12/22/20    9:29:28  9:29:28:15  00:10:28.0  102  09:39:56:15
2   2/17/21    9:20:20   9:20:20:2   0:00:05.2  206   09:20:25:4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM