[英]Pandas dataframe col conversion to timedelta to use resample
I am trying to simply change the TimeStamp column to timedelta objects so that I can use resample to average over 2 min intervals. 我试图将TimeStamp列更改为timedelta对象,以便可以使用重采样在2分钟的间隔内进行平均。
The column name can vary depending on device software, so I assigned it to a variable but the code isn't working 列名称可能会因设备软件而异,因此我将其分配给了一个变量,但是代码不起作用
Sample dataset: 样本数据集:
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
The code I tried is as follows: 我尝试的代码如下:
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)
Use pd.to_timedelta
使用
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
Without setting the index 不设置索引
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.