Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.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_Numpy_Time Series - Fatal编程技术网

Python 计算熊猫的高-低

Python 计算熊猫的高-低,python,pandas,numpy,time-series,Python,Pandas,Numpy,Time Series,这是我的数据集,索引为日期和价格列。我想在这里创建一个列参数(param),它在comments列中如下所示: Index Price | param Comments (P is Price) 1989-01-24 68.800 0 P < P-1 (P-1 doesnt exist so 0) par

这是我的数据集,索引为日期和价格列。我想在这里创建一个列参数(param),它在comments列中如下所示:

 Index          Price   |   param            Comments (P is Price)
1989-01-24      68.800      0               P <  P-1 (P-1 doesnt exist so 0)
                                            param = 0 , 

1989-01-25      68.620     -2               P < P-1 check P<P-2(P-2 doesnt 
                                            exist so P is a 2 day low and 
                                            param = -2 

1989-01-26      68.930      3               P > P-1, P>P-2, P-3(doesnt exist
                                            So P is a 3 day high, param =3 


1989-01-27      68.9900     4               P > P-1 > P-2 > P -3 and hence a 
                                            4 day high, param = 4                                            

1989-01-30      69.11       5               P > P-1> P-2 > P-3 > P-4 and 
                                            hence a 5 day high, param = 5

1989-01-31      69.070     -2               P < P-1 > P-2 and hence a 2 day 
                                            low, param = -2 
指数价格|参数注释(P为价格)
1989-01-24 68.800 PP-2,P-3(不存在
所以P是3天的高点,param=3
1989-01-2768.99004P>P-1>P-2>P-3,因此
4天高点,参数=4
1989-01-30 69.11 5 P>P-1>P-2>P-3>P-4和
因此为5天高点,参数=5
1989年01月31日69.070-2 PP-2,因此为2天
低,参数=-2

有人能告诉我在熊猫中实现这一点的优雅方法吗?

根据您的评论部分,我的理解是,
param
列实际上是我们获得的
Price
列的值的排名。这类似于在inte流中查找特定值的排名GER作为输入。这可以使用PriorityQueue实现。您需要创建一个带有比较器的优先级队列,该比较器将以元素值的递增顺序存储优先级队列中的元素。要查找秩,您只需迭代队列并在列中查找最近元素的索引。这将但是,查找元素索引需要花费O(n)个时间。请查看下面的python文档,了解如何在python中创建heapq或优先级队列:

如果要在O(logn)中执行此操作,可以使用自平衡BST,如AVL或红黑树。最近输入的元素的值的排名将是其左侧的索引。在最坏的情况下,这可以在O(logn)时间内完成。python中AVL的详细信息:


您想要的是使用偏移量进行分组和排序。熊猫包括了所有这些

这里有一个有效的线性解决方案:

df=pd.DataFrame({'price':rand(15)})
df['ascending']=df.price<df.price.shift()
df['slope']=(-1)**df.ascending
df['group']=df.ascending.diff().abs().cumsum()
df['pseudorank']=df.slope.cumsum()
offset=df.groupby('group',sort=False).pseudorank.first()
df['param']=(df.pseudorank-df.join(offset,'group',lsuffix='old').pseudorank+2*df.slope)
df.param=df.param.fillna(0).astype(int)

我创建了很多列用于解释,如果需要,您可以删除它们。

形成
param
列的基础是什么?@piRSquared您能帮我吗?我仍在努力理解这意味着什么!如果您能提供更多帮助,我将不胜感激
       price ascending  slope group  pseudorank  param
0   0.160806     False      1   NaN           1      0
1   0.068664      True     -1     1           0     -2
2   0.663227     False      1     2           1      2
3   0.273134      True     -1     3           0     -2
4   0.610329     False      1     4           1      2
5   0.595016      True     -1     5           0     -2
6   0.975163     False      1     6           1      2
7   0.692874      True     -1     7           0     -2
8   0.682642      True     -1     7          -1     -3
9   0.337418      True     -1     7          -2     -4
10  0.307546      True     -1     7          -3     -5
11  0.462594     False      1     8          -2      2
12  0.304216      True     -1     9          -3     -2
13  0.189434      True     -1     9          -4     -3
14  0.865468     False      1    10          -3      2