确定两个 pandas 时间序列相交的坐标,以及时间序列相交多少次 [英] determine the coordinates where two pandas time series cross, and how many times the time series cross

查看:114
本文介绍了确定两个 pandas 时间序列相交的坐标,以及时间序列相交多少次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要绘制两个时间序列的图,可以说它们都具有从左到右的上升趋势,那么是否可以使用熊猫来发现两条线相交的位置以及相交的频率?

If I were to graph two time series, let's say they both have an upward positive trend from left to right, is there anyway to use pandas to find where the two lines intersect and how often?

例如:

两个时间序列相交的频率: 1

相交点的坐标是什么:大约x轴:1955 y轴:7

What are the coordinates of the intersecting points: approx x-axis: 1955 y-axis: 7

推荐答案

这里是使用熊猫的快速尝试.

Here is a quick try using pandas.

import pandas as pd
import numpy as np

df = pd.DataFrame({"A":[1,2,3,4,5], "B":[0.5,3,1,1,6]})
print df

哪个给

   A    B
0  1  0.5
1  2  3.0
2  3  1.0
3  4  1.0
4  5  6.0

然后使用差异

df['difference'] = df.A - df.B

df['cross'] = np.sign(df.difference.shift(1))!=np.sign(df.difference)
np.sum(df.cross)-1

第一行需要-1,shift(1)将在其中返回NaN

You need the -1 for the first row in which the shift(1) will return NaN

这篇关于确定两个 pandas 时间序列相交的坐标,以及时间序列相交多少次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆