我的问题很简单,但我就是无法理解它: 我有两个数据帧:
- 具有两列的时间序列
dataframe
:Timestamp
和DataValue
- 具有
start
、end
时间戳和标签的时间间隔dataframe
我想做什么:
向时间序列添加第三列,该列根据时间间隔dataframe
yields
标签。
每个timepoint
都需要有一个由时间间隔指定的分配标签dataframe
。
此代码有效:
TimeSeries_labelled = TimeSeries.copy(deep=True)
TimeSeries_labelled["State"] = 0
for index in Timeintervals_States.index:
for entry in TimeSeries_labelled.index:
if Timeintervals_States.loc[index,"start"] <= TimeSeries_labelled.loc[entry, "Timestamp"] <= Timeintervals_States.loc[index,"end"]:
TimeSeries_labelled.loc[entry, "State"] = Timeintervals_States.loc[index,"state"]
但它真的很慢。我试图用内置的 pyhton 过滤器代码让它更短、更快,但惨遭失败。 请帮忙!
我真的不了解时间序列,使用包含时间戳的数据帧作为日期时间对象,您可以使用如下所示的内容:
import pandas as pd
#Create the thrid column in the target dataframe
df_timeseries['label'] = pd.Series('',index=df_timeseries.index)
#Loop over the dataframe containing start and end timestamps
for index,row in df_start_end.iterrows():
#Create a boolean mask to filter data
mask = (df_timeseries['timestamp'] > row['start']) & (df_timeseries['timestamp'] < row['end'])
df_timeseries.loc[mask,'label'] = row['label']
这将使与掩码条件匹配的时间序列数据帧具有行的标签,对于包含开始和结束时间戳的数据帧的每一行