Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Resample and merge multiple time series with Pandas
# Resample time series
# Omid (Feb 2017)
import pandas as pd
import numpy as np
FILENAME_A = '2015_27_11-12_7_15---improv#1@acc+antonio+right.txt'
FILENAME_B = '2015_27_11-12_7_15---improv#1@acc+bevin+right.txt'
FILENAME_V = '2015_27_11-12_7_15---improv#1@acc+vanessa+right.txt'
OUT_DIR = 'resampled'
SAMPLE_RATE = '120mS'
DATE_COEF = 1000000
COLS = [1,2,3]
input_a = pd.read_csv('clean/%s'%(FILENAME_A),delim_whitespace=True, header=None)
input_b = pd.read_csv('clean/%s'%(FILENAME_B),delim_whitespace=True, header=None)
input_v = pd.read_csv('clean/%s'%(FILENAME_V),delim_whitespace=True, header=None)
def resample_series(input_csv, cols):
result = []
for col in cols:
# create a data indexed series for each column
series = pd.Series(input_csv[col].as_matrix(), index=pd.to_datetime(input_csv[0]*DATE_COEF))
# resample the series
new_series = series.resample(SAMPLE_RATE).bfill()
result.append(new_series)
return result
def merge_series3(input_series):
return pd.merge(input_series[0].to_frame(), input_series[1].to_frame(),left_index=True, right_index=True).merge(input_series[2].to_frame(),left_index=True, right_index=True)
a = resample_series(input_a,COLS)
b = resample_series(input_b,COLS)
v = resample_series(input_v,COLS)
# print(a[0].shape)
# print(b[0].shape)
# print(v[0].shape)
a_all = merge_series3(a)
b_all = merge_series3(b)
v_all = merge_series3(v)
a_all.to_csv(OUT_DIR+'/improv1_acc_right_antonio.csv', header=['x','y','z'], index_label='timestamp')
b_all.to_csv(OUT_DIR+'/improv1_acc_right_bevin.csv', header=['x','y','z'], index_label='timestamp')
v_all.to_csv(OUT_DIR+'/improv1_acc_right_vanessa.csv', header=['x','y','z'], index_label='timestamp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment