重置数据帧中的时间戳,并计算时间



dataframe中的时间戳是这样的

In [18]: df['time'].head(1)
Out[18]: 2021-10-05 12:00:00.000000+00:00
In [19]: df['time'].tail(1)
Out[19]: 2021-10-07 12:00:00.000000+00:00

我想要一个新的列(time_new)。第一个时间戳必须重置为0,所以起始点是0。然后它只计算小时、秒和毫秒。所以在这个案例中最后有48小时。下表为说明

| index     | time                              | time_new      |
|-------    |---------------------------------- |----------     |
| 0         | 2021-10-05 12:00:00.000000+00:00  | 00:00:00.000  |
| 1         | 2021-10-05 13:00:00.000000+00:00  | 01:00:00.000  |
| 2         | 2021-10-05 13:00:00.001000+00:00  | 01:00:00.001  |
| 3         | 2021-10-06 02:00:00.000000+00:00  | 14:00:00.000  |
| 4         | 2021-10-07 12:00:00.000000+00:00  | 48:00:00.000  |

对于timedeltas,减去column的最小值:

df['time_new'] = df['time'].sub(df['time'].min())

或列的第一个值:

df['time_new'] = df['time'].sub(df['time'].iat[0])

如果格式很重要,使用自定义函数:

def format_timedelta(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('{:02d}:{:02d}:{:.3f}').format(int(hours), int(minutes), seconds) 
df['time_new'] = df['time'].sub(df['time'].min()).apply(format_timedelta)
print (df)
time     time_new
0        2021-10-05 12:00:00+00:00  00:00:0.000
1        2021-10-05 13:00:00+00:00  01:00:0.000
2 2021-10-05 13:00:00.001000+00:00  01:00:0.001
3        2021-10-06 02:00:00+00:00  14:00:0.000
4        2021-10-07 12:00:00+00:00  48:00:0.000

Formean:

avg = df.loc[df['time_new'] <= pd.Timedelta('01:00:00'), 'vibration'].mean()

相关内容

  • 没有找到相关文章