我有一个逗号分隔的数据文件,如下所示:
ID | StartTimeStamp | EndTimeStamp | Duration (in seconds) | AssetName
1233 | 2017-01-01 00:00:02 | 2017-01-01 00:10:01 | 601 | Car1
1233 | 2017-01-01 00:10:01 | 2017-01-01 00:10:12 | 11 | Car1
...
1235 | 2017-01-01 00:00:02 | 2017-01-01 00:10:01 | 601 | CarN
等等
现在,我想使用starttime和duration来创建以下内容以对数据进行上采样。
ID | StartTimeStamp | AssetName
1233 | 2017-01-01 00:00:02 | Car1
1233 | 2017-01-01 00:00:03 | Car1
1233 | 2017-01-01 00:00:04 | Car1
...
1233 | 017-01-01 00:10:01 | Car1
...
1235 | 2017-01-01 00:00:02 | CarN
1235 | 2017-01-01 00:00:03 | CarN
1235 | 2017-01-01 00:00:04 | CarN
... (i.e. 601 rows of data one per second)
1235 | 2017-01-01 00:10:01 | CarN
但我增加了如何做到这一点的几率,因为上采样似乎只能与时间序列一起工作?我正在考虑使用StartTimeStamp
和文件中的秒数来使用for循环,但是如何解决这个问题却不知所措?
您可以为每个ID
组重新采样,然后填充字符列中的间隙
import pandas as pd
df_resampled = df.set_index(pd.to_datetime(df.StartTimeStamp)).groupby('ID')
# Expand out the dataframe for one second
df_resampled = df_resampled.resample('1S').asfreq()
# Interpolate AssetName for each group
df_resampled['AssetName'] = df_resampled['AssetName'].ffill().bfill()