我的df:
In [163]: df.head()
Out[163]:
x-axis y-axis z-axis
time
2017-07-27 06:23:08 -0.107666 -0.068848 0.963623
2017-07-27 06:23:08 -0.105225 -0.070068 0.963867
.....
我将索引设置为datetime。由于采样率(10 Hz)在数据帧中并不总是恒定的,并且在某些时候我有8或9个样本。
如何在熊猫中做到这一点?
答案 0 :(得分:0)
首先,让我们创建一些可能类似于您的数据的样本数据。
import pandas as pd
from datetime import timedelta
from datetime import datetime
base = datetime.now()
date_list = [base - timedelta(days=x) for x in range(0, 2)]
values = [v for v in range(2)]
df = pd.DataFrame.from_dict({'Date': date_list, 'values': values})
df = df.set_index('Date')
df
values
Date
2017-08-18 20:42:08.563878 0
2017-08-17 20:42:08.563878 1
现在我们将创建另一个数据帧,每100毫秒的数据点。
min_val = df.index.min()
max_val = df.index.max()
all_val = []
while min_val <= max_val:
all_val.append(min_val)
min_val += timedelta(milliseconds=100)
# len(all_val) 864001
df_new = pd.DataFrame.from_dict({'Date': all_val})
df_new = df_new.set_index('Date')
final_df = df_new.join(df)
final_df
values
Date
2017-08-17 20:42:08.563878 1.0
2017-08-17 20:42:08.663878 NaN
2017-08-17 20:42:08.763878 NaN
2017-08-17 20:42:08.863878 NaN
2017-08-17 20:42:08.963878 NaN
2017-08-17 20:42:09.063878 NaN
2017-08-17 20:42:09.163878 NaN
现在插入数据:
df_final.interpolate()
values
Date
2017-08-17 20:42:08.563878 1.000000
2017-08-17 20:42:08.663878 0.999999
2017-08-17 20:42:08.763878 0.999998
2017-08-17 20:42:08.863878 0.999997
2017-08-17 20:42:08.963878 0.999995
2017-08-17 20:42:09.063878 0.999994
2017-08-17 20:42:09.163878 0.999993
2017-08-17 20:42:09.263878 0.999992
一些插值策略:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.interpolate.html
更新:根据评论中的讨论:
说我们的初始数据没有毫秒信息。
df_new_date_without_miliseconds = df_new['Date']
df_new_date_without_miliseconds[0] # Timestamp('2017-08-17 21:45:49')
max_value_date = df_new_date_without_miliseconds[0]
max_value_miliseconds = df_new_date_without_miliseconds[0]
updated_dates = []
for val in df_new_date_without_miliseconds:
if val == max_value_date:
val = max_value_miliseconds + timedelta(milliseconds=100)
max_value_miliseconds = val
elif val > max_value_date:
max_value_date = val + timedelta(milliseconds=0)
max_value_miliseconds = val
updated_dates.append(val)
output:
[Timestamp('2017-08-17 21:45:49.100000'),
Timestamp('2017-08-17 21:45:49.200000'),
Timestamp('2017-08-17 21:45:49.300000'),
Timestamp('2017-08-17 21:45:50'),
Timestamp('2017-08-17 21:45:50.100000'),
将新值分配给DataFrame
df_new['Date'] = updated_dates