datetime - Merge two time series with offset? -
i have 2 files time series data, this:
file_a.csv: t,x,y,z 00:00:00,1,1,1 00:00:01,2,2,2 00:00:02,3,3,3 00:00:03,4,4,4 file_b.csv: t,x,y,z 00:00:00,5,5,5 00:00:01,6,6,6 00:00:02,7,7,7
and merge them in order get:
t,x,y,z 00:00:00,1,1,1 00:00:01,2,2,2 00:00:02,3,3,3 00:00:03,4,4,4 00:00:04,5,5,5 00:00:05,6,6,6 00:00:06,7,7,7
basically, want offset "t" of data set n+1 last value of "t" of data set n.
how can that? combine_first not want: merges 2 columns.
needed guess stuff in question (exact format of time, times sorted, increments fixed), here general idea.
starting dataframes:
import pandas pd stringio import stringio import numpy np = pd.read_csv(stringio('t,x,y,z\n00:00:00,1,1,1\n00:00:01,2,2,2\n00:00:02,3,3,3\n00:00:03,4,4,4')) b = pd.read_csv(stringio('t,x,y,z\n00:00:00,5,5,5\n00:00:01,6,6,6\n00:00:02,7,7,7'))
convert times , sort (possibly can skip last part):
a.t = pd.to_datetime(a.t) a.sort(columns=[a.t.name], inplace=true) b.t = pd.to_datetime(b.t) b.sort(columns=[b.t.name], inplace=true)
increment each time in b
difference between , first time in b
+ last time in a
plus 1 sec.
b.t = b.t - b.t[0] + a.t.values[-1] + np.timedelta64(1, 's')
finally, concat:
pd.concat([a, b])
(note times here full datetime
s now, i.e., dates.)
Comments
Post a Comment