Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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/matlab/15.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 如何有效地组合两个时间序列以进行交替?_Python_Pandas_Time Series - Fatal编程技术网

Python 如何有效地组合两个时间序列以进行交替?

Python 如何有效地组合两个时间序列以进行交替?,python,pandas,time-series,Python,Pandas,Time Series,比如说,我有两个非常长的系列——大系列和小系列 index = pd.date_range(start='1952', periods=10**6, freq='s') big = pd.Series(np.ones(len(index))*97, index) small = pd.Series(np.ones(len(index))*2, index) 我想要实现的是创建一个新的系列,它将大的和小的组合在一起,在它们的值之间交替,使用边框来确定何时切换到另一个(例如,每5秒有一个边框) 是

比如说,我有两个非常长的系列——大系列和小系列

index = pd.date_range(start='1952', periods=10**6, freq='s')
big = pd.Series(np.ones(len(index))*97, index)
small = pd.Series(np.ones(len(index))*2, index)
我想要实现的是创建一个新的系列,它将大的和小的组合在一起,在它们的值之间交替,使用
边框来确定何时切换到另一个(例如,每5秒有一个边框)

是否有一个有效的基于矩阵的操作组合可以用来实现这一点?我尝试查看中的各种连接、合并等运算符,但找不到任何提供类似逻辑的运算符

我可以使用for循环实现这一点,但即使对于一系列
len()
10ˆ5,这也会持续一分钟以上

alternating = pd.Series()
for i in range(1, 100, 2):
    b0 = borders[i-1]
    b1 = borders[i]
    b2 = borders[i+1]
    sec = pd.offsets.Second(1)
    alternating = alternating.append(small[b0:b1-sec]).append(big[b1:b2-sec])
alternative.head(24)


如果您的经期只有一分钟,您可以尝试以下方法:

index = pd.date_range(start='1952', periods=10**6, freq='s')
big = pd.Series(np.ones(len(index))*97, index)
small = pd.Series(np.ones(len(index))*2, index)

alternating = big[big.index.second % 10 >= 5].combine_first(small)

alternative
看起来完全符合您的要求,在150毫秒内计算出来。

太好了,这就是我需要的!奇怪的是,我知道这些方法是各自独立的,但要在心里把它们结合起来并不是那么容易——首先想到的是一个结构化的、基于循环的算法。。。要更改时间间隔,您只需将条件稍微复杂一点-例如,
big.index.second/17%2==0
持续17秒。顺便说一句,注意您的回复速度非常快。有你正在使用的应用程序吗?我尝试通过stackexchange订阅一些标签,但我只收到每日电子邮件摘要。有更快的方法吗?17秒不是一分钟的一小部分,因此这不会像期待的那样起作用(0..16,17..33,34..50,51..59,最后一个间隔更短)。但是你可以用它来计算某个固定日期时间的总秒数的时间增量,或者使用行号。是的,很好的定位。到目前为止,我得到的是
start=big.index[0]。to_pydatetime()
alternative=big[(big.index.to_pydatetime()-start)。total_seconds()/17%2==0]
,但我似乎找不到一种方法将total_seconds()调用映射到所有元素。@kermit666,
big[(big.index.astype(np.int64)/10**9)%34>=17]如何。首先组合_(小)
1952-01-16 00:00:00     2
1952-01-16 00:00:01     2
1952-01-16 00:00:02     2
1952-01-16 00:00:03     2
1952-01-16 00:00:04     2
1952-01-16 00:00:05    97
1952-01-16 00:00:06    97
1952-01-16 00:00:07    97
1952-01-16 00:00:08    97
1952-01-16 00:00:09    97
1952-01-16 00:00:10     2
1952-01-16 00:00:11     2
1952-01-16 00:00:12     2
1952-01-16 00:00:13     2
1952-01-16 00:00:14     2
1952-01-16 00:00:15    97
1952-01-16 00:00:16    97
1952-01-16 00:00:17    97
1952-01-16 00:00:18    97
1952-01-16 00:00:19    97
1952-01-16 00:00:20     2
1952-01-16 00:00:21     2
1952-01-16 00:00:22     2
1952-01-16 00:00:23     2
index = pd.date_range(start='1952', periods=10**6, freq='s')
big = pd.Series(np.ones(len(index))*97, index)
small = pd.Series(np.ones(len(index))*2, index)

alternating = big[big.index.second % 10 >= 5].combine_first(small)