我试图简单地将TimeStamp列更改为timedelta对象,以便我可以使用 resample 平均超过2分钟。
列名可能因设备软件而异,因此我将其分配给变量,但代码不能正常工作
示例数据集:
TimeStamp 340 341 342
10:27:30 1.953036 2.110234 1.981548
10:28:30 1.973408 2.046361 1.806923
10:29:30 0 0 0.014881
10:30:30 2.567976 3.169928 3.479591
我尝试的代码如下:
import pandas as pd
from datetime import datetime
def time_based_average(dataframe, duration):
df_resampled = dataframe.resample(str(duration) + 'min').mean()
return df_resampled
# Reading data as pandas dataframes
path = '/Users/Desktop/Model/'
file_1 = 'SR Lamp.csv'
df_1 = pd.read_csv(path + file_1, skipinitialspace = True)
# Determine col label for timestamps
time_lab_1 = df_1.columns[0]
# Converting times to timedalta objects
pd.to_timedelta(df_1[time_lab_1])
# Average every 2min for the device
df_1_resampled = time_based_average(df_1, 2)
答案 0 :(得分:2)
使用pd.to_timedelta
df.index = pd.to_timedelta(df.index)
df.resample('2T').mean()
340 341 342
TimeStamp
10:27:30 1.963222 2.078298 1.894235
10:29:30 1.283988 1.584964 1.747236
10:31:30 NaN NaN NaN
不设置索引
df.TimeStamp = pd.to_timedelta(df.TimeStamp)
df.resample('2T', on='TimeStamp').mean().reset_index()
TimeStamp 340 341 342
0 10:27:30 1.963222 2.078298 1.894235
1 10:29:30 1.283988 1.584964 1.747236
2 10:31:30 NaN NaN NaN