Skip to content

Instantly share code, notes, and snippets.

@viniroger
Created October 15, 2019 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viniroger/b555fe1269f90f66fb77eb7ba8f2c05c to your computer and use it in GitHub Desktop.
Save viniroger/b555fe1269f90f66fb77eb7ba8f2c05c to your computer and use it in GitHub Desktop.
Read CSV with timestamp and calculate 5-min average
def str_to_datetime(s, format_in):
"""
Convert all elements of a pandas series into datetime format
"""
import time
from datetime import datetime
from time import mktime
# Convert from day of year from strptime structure
strptime_object = s.apply(lambda x: time.strptime(x, format_in))
# Create datetime object
dt = strptime_object.apply(lambda x: datetime.fromtimestamp(mktime(x)))
return(dt)
import pandas as pd
df = pd.read_csv('filename.csv')
# Create timestamp index
dt = str_to_datetime(df['Timestamp'], '%Y-%m-%d %H:%M')
df['Timestamp'] = dt
df.set_index('Timestamp', inplace = True)
# Calculate means each 5 minutes
df_means = df.resample('5Min').mean()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment