简体   繁体   中英

R merge two different time series with same date

I have csv file with data. Link is here. Granularity of time series is 5 min for year 2013. However, values are missing for some time stamps.

I want to create a time series with 5 minute interval with value zero for time stamps which are missing.

Please advise how to do this either in Pandas or Python

In pandas, you just join on the index:

from io import StringIO

import numpy as np
import pandas

ts1_string = StringIO("""\
V1,V2
01/01/2013 00:05:00,10
01/01/2013 00:10:00,6
01/01/2013 00:15:00,10
01/01/2013 00:25:00,8
01/01/2013 00:30:00,11
01/01/2013 00:35:00,7""")

ts2_string = StringIO("""
V1,V2
2013-01-01 00:00:00,0
2013-01-01 00:05:00,0
2013-01-01 00:10:00,0
2013-01-01 00:15:00,0
2013-01-01 00:20:00,0
2013-01-01 00:25:00,0""")

ts1 = pandas.read_csv(ts1_string, parse_dates=True, index_col='V1')
ts2 = pandas.read_csv(ts2_string, parse_dates=True, index_col='V1')

# here's where the join happens
# (suffixes deal with overlapping column names)
ts_joined = ts1.join(ts2, rsuffix='_ts1', lsuffix='_ts2')

# and finally
print(ts_joined.head())

Which gives:

                     V2_ts2  V2_ts1
V1                                 
2013-01-01 00:05:00      10       0
2013-01-01 00:10:00       6       0
2013-01-01 00:15:00      10       0
2013-01-01 00:25:00       8       0
2013-01-01 00:30:00      11     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