【问题标题】:How to track consecutive highs in a Pandas Series?如何追踪熊猫系列的连续高点?
【发布时间】:2021-04-14 21:29:58
【问题描述】:

我想用熊猫在时间序列中跟踪连续高点,如图所示。见下图:

如何用 Pandas 做到这一点?

如果您想玩一个现实生活中的示例,您可以下载股票价格,例如“MSFT”并在示例中使用“收盘价”。下载股票价格的方法有多种,这里有一种:

import yahooquery

ticker = Ticker('MSFT', asynchronous=True)

df = ticker.history()

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    返回的数据不足以执行您想要的操作。如果数据集确实具有连续的局部最大值,则使用 SO find min/max 技术可以通过分析 max 列来工作。

    import yahooquery
    import matplotlib.pyplot as plt
    import pandas as pd, numpy as np
    from scipy.signal import argrelextrema
    
    ticker = yahooquery.Ticker('MSFT', asynchronous=True)
    
    df = ticker.history()
    df = df.reset_index()
    
    n = 5
    df['min'] = df.iloc[argrelextrema(df.close.values, np.less_equal,
                        order=n)[0]]['close']
    df['max'] = df.iloc[argrelextrema(df.close.values, np.greater_equal,
                        order=n)[0]]['close']
    
    plt.scatter(df["date"], df['min'], c='r')
    plt.scatter(df["date"], df['max'], c='g')
    plt.plot(df["date"], df["close"])
    plt.show()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 2021-08-11
      • 2019-05-09
      • 2019-04-04
      • 2018-10-30
      相关资源
      最近更新 更多