有一个具有时间序列的数据帧:
import pandas as pd
import numpy as np
df = pd.DataFrame({'times': np.array(['1994-07-25 15:00:00.000',
'1994-07-25 16:00:00.000',
'1994-07-26 18:45:00.000',
'1994-07-27 15:15:00.000',
'1994-07-27 16:00:00.000',
'1994-07-28 18:45:00.000',
'1994-07-28 19:15:00.000',
], dtype='datetime64'),
'diff': [0.0,0.03,0.04,0.05,0,0.01,0.0,]})
(两个信号之间的)差异恢复为零,我想找出它超出时间的时间-即非零的时间->低于所需结果
df['deviation_time_delta'] = pd.to_timedelta(['nan',
'nan',
'nan',
'nan',
'2 days 00:00:00.000',
'nan',
'0 days 00:30:00.000',
])
我已经尝试过-但是它并不漂亮,并且不能在任意长度偏差上工作:
df['diff_1'] = df['diff'].shift(1)
df['diff_2'] = df['diff'].shift(2)
df['diff_3'] = df['diff'].shift(3)
df['diff_4'] = df['diff'].shift(4)
df['times_1'] = df['times'].shift(1)
df['times_2'] = df['times'].shift(2)
df['times_3'] = df['times'].shift(3)
df['times_4'] = df['times'].shift(4)
def calc_dev_time_delta(cur_diff, diff_1, diff_2, diff_3, diff_4, cur_time, time_1, time_2, time_3, time_4):
if cur_diff != 0.0: return np.nan
if diff_1 == 0.0: return np.nan
if diff_2 == 0.0: return cur_time - time_1
if diff_3 == 0.0: return cur_time - time_2
if diff_4 == 0.0: return cur_time - time_3
df['dev_time_delta'] = df.apply(lambda row: calc_dev_time_delta(row['diff'], row['diff_1'], row['diff_2'],row['diff_3'],row['diff_4'], row['times'], row['times_1'], row['times_2'], row['times_3'], row['times_4']), axis=1)
您知道更好/更干净的方法来实现此结果吗?
答案 0 :(得分:1)
如果我理解正确,您想计算与diff
为0的上一行的差异。
使用groupby
和diff
df
diff times
0 0.00 1994-07-25 15:00:00
1 0.03 1994-07-25 16:00:00
2 0.04 1994-07-26 18:45:00
3 0.05 1994-07-27 15:15:00
4 0.00 1994-07-27 16:00:00
5 0.01 1994-07-28 18:45:00
6 0.00 1994-07-28 19:15:00
df['deviation_time_delta'] = df.groupby('diff')['times'].diff()
df['deviation_time_delta'].loc[df['diff']!=0] = 0
df
diff times deviation_time_delta
0 0.00 1994-07-25 15:00:00 NaT
1 0.03 1994-07-25 16:00:00 0
2 0.04 1994-07-26 18:45:00 0
3 0.05 1994-07-27 15:15:00 0
4 0.00 1994-07-27 16:00:00 2 days 01:00:00
5 0.01 1994-07-28 18:45:00 0
6 0.00 1994-07-28 19:15:00 1 days 03:15:00
答案 1 :(得分:1)
我不确定我了解你想要什么,但这不是做这份工作吗?
dfZero = df[df['diff'] == 0]
dfZero['deltaT'] = dfZero.times.diff()
df = df.merge(dfZero, how='left')
print(df)
输出:
times diff deltaT
0 1994-07-25 15:00:00 0.00 NaT
1 1994-07-25 16:00:00 0.03 NaT
2 1994-07-26 18:45:00 0.04 NaT
3 1994-07-27 15:15:00 0.05 NaT
4 1994-07-27 16:00:00 0.00 2 days 01:00:00
5 1994-07-28 18:45:00 0.01 NaT
6 1994-07-28 19:15:00 0.00 1 days 03:15:00
答案 2 :(得分:0)
受godot的回答和评论启发,请在下面查看我最终得到的解决方案:
df['diff_1'] = df['diff'].shift(1)
def keep_row(cur_diff, prev_diff):
return cur_diff == 0.0 or prev_diff == 0.0
df['keep'] = df.apply(lambda row: keep_row(row['diff'], row['diff_1']), axis=1)
df_short = df[df['keep']]
df_short = df_short.drop(['diff_1'], axis=1)
df_short['diff_1'] = df_short['diff'].shift(1)
df_short['times_1'] = df_short['times'].shift(1)
def calc_deviation_time(cur_diff, prev_time, cur_time):
if cur_diff != 0.0: return np.nan
return cur_time - prev_time
df_short['deviation_time'] = df_short.apply( lambda row: calc_deviation_time(row['diff'], row['times_1'], row['times']), axis=1)
df_short = df_short.drop(['keep', 'diff_1', 'times_1'], axis=1)
df_short