Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python R合并具有相同日期的两个不同时间序列_Python_R_Pandas_Merge_Zoo - Fatal编程技术网

Python R合并具有相同日期的两个不同时间序列

Python R合并具有相同日期的两个不同时间序列,python,r,pandas,merge,zoo,Python,R,Pandas,Merge,Zoo,我有一个包含数据的csv文件。2013年的时间序列粒度为5分钟。但是,某些时间戳缺少值 我想创建一个间隔为5分钟的时间序列,缺失的时间戳的值为零 请建议如何在Pandas或Python中执行此操作在Pandas中,您只需加入索引: 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

我有一个包含数据的csv文件。2013年的时间序列粒度为5分钟。但是,某些时间戳缺少值

我想创建一个间隔为5分钟的时间序列,缺失的时间戳的值为零


请建议如何在Pandas或Python中执行此操作在Pandas中,您只需加入索引:

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())
其中:

                     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

您想要熊猫或R的解决方案吗?这是这里要明确的一点。我对任何一种解决方案都没有意见ok,如果你想得到有用的回答,你需要做以下几点:1在问题的前面非常清楚,2正确地标记这个问题,3包括人们可以用来乱弄你的数据的复制/可复制的示例。不一定是所有的数据-只是证明概念所需的最小数量,我想我已经按照你的要求做了。我确实提到了如何使用zoo或Python Pandas合并TS1和TS2?我的表格是可复制/可复制的示例。如果缺少什么,请告诉我。谢谢,不太感谢。您发布的大多数输出都不是很有用,并且您的标记没有提到python、R或zoo,我将添加它们。请参阅我的回答,了解一个自包含、可复制的示例是什么样子的。你能用TS1和TS2吗。您已经生成了随机数。但是我已经有TS1了。我认为问题在于TS1和TS2中的日期时间语法。您能提供一些代码来生成TS1和TS2吗@Chandra@Chandra怎么样?一般来说,人们只愿意在解决问题时稍微少做一些工作。将TS1复制到TS1.csv文件和python中-dat=pd.read_csvTS1.csv index=pd.DatetimeIndexstart='2013-01-01 00:00',end='2014-01-01 00:00',freq='5 min'df=pd.DataFrameindex=index TS1.to_csvQ1.csv,日期\格式=%d/%m/%Y%H:%m:%S df.to\ csvQ2.csv,日期\格式=%Y/%m/%d%H:%m:%Sdf=pd.DataFrameindex=index@Chandra这就是你的问题所在!一旦你的评论被删除