Created
October 15, 2019 11:38
-
-
Save viniroger/b555fe1269f90f66fb77eb7ba8f2c05c to your computer and use it in GitHub Desktop.
Read CSV with timestamp and calculate 5-min average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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