简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM