【问题标题】:Is there any good way to group time series Stock Data?有什么好方法可以对时间序列股票数据进行分组吗?
【发布时间】:2020-11-07 14:37:05
【问题描述】:

我在对具有自定义时间范围的股票市场数据进行分组时遇到了问题。 原始数据如下所示。 我想在 2 小时内分组。 由于我的数据的开始时间是上午 9:15,我想将特定日期的 9:15:00 到 11:15:00 数据分组。特定日期的数据也在 15:15:00 结束。 但这样做时面临一些问题。如下所述。

Actual Raw Data
TIME                 Open   High    Low     CLOSE   VOLUME
2019-10-31 09:15:00 235.70  236.50  234.40  234.55  1306585 
2019-10-31 10:15:00 234.55  235.05  233.40  234.00  765419      
2019-10-31 11:15:00 234.00  235.25  233.65  234.10  664682      
2019-10-31 12:15:00 234.10  235.75  234.00  235.70  676993      
2019-10-31 13:15:00 235.75  236.15  234.25  235.25  527381      
2019-10-31 14:15:00 235.25  235.80  227.10  227.45  2416364     
2019-10-31 15:15:00 227.50  228.00  227.05  227.70  613380      
2019-11-01 09:15:00 227.70  232.45  227.50  231.85  1844851     
2019-11-01 10:15:00 231.95  233.05  231.05  232.20  1000537     
2019-11-01 11:15:00 232.20  232.65  231.05  232.60  454966      
2019-11-01 12:15:00 232.60  233.50  231.40  231.70  569539      
2019-11-01 13:15:00 231.70  236.45  231.40  235.35  1388397 
Code Used
df['TIME'] = pd.to_datetime(df['TIME'])
df = df.set_index("TIME")
df = df.groupby(pd.Grouper(freq='2H')).agg({"Open": "first", 
                                             "CLOSE": "last", 
                                             "Low": "min", 
                                             "High": "max"})
OUTPUT
Using the above code I am getting wrong information and calculations as below
TIME                 Open    CLOSE  Low      High
2019-10-31 08:00:00 235.70  234.55  234.40  236.50
2019-10-31 10:00:00 234.55  234.10  233.40  235.25
2019-10-31 12:00:00 234.10  235.25  234.00  236.15
2019-10-31 14:00:00 235.25  227.70  227.05  235.80
2019-10-31 16:00:00 NaN NaN NaN NaN
2019-10-31 18:00:00 NaN NaN NaN NaN
2019-10-31 20:00:00 NaN NaN NaN NaN
2019-10-31 22:00:00 NaN NaN NaN NaN
2019-11-01 00:00:00 NaN NaN NaN NaN
2019-11-01 02:00:00 NaN NaN NaN NaN

我希望这些数据以特定日期的 9:15:00 开始并以 15:15 结束。还想避免任何可能影响我下一个日期的 2Hour 数据计算的 NAN 值

我在互联网上搜索了很多地方并浏览了文档但无法解决这个问题,同时我也想对 5 分钟数据进行相同的计算,以每小时或自定义的分钟间隔数据保存股市的时机记在心里。 你能帮我解决这个问题吗?

【问题讨论】:

    标签: python pandas dataframe date


    【解决方案1】:

    您可以使用resample,指定on 日期时间列TIME 而不是索引,并使用origin='start'TIME 列中的第一个值上开始重新采样的时间序列。然后只需删除 null 值。

    如果您已经通过df = df.set_index("TIME") 设置了数据帧的索引,那么您可以将on='TIME' 从参数中删除到resample,因为默认情况下对索引进行重采样。

    >>> df.resample('2H', on='TIME', origin='start').agg(
        {"Open": "first", 
         "CLOSE": "last", 
         "Low": "min", 
         "High": "max"}
    ).dropna()[['Open', 'High', 'Low', 'CLOSE']]
                           Open    High     Low   CLOSE
    TIME                                               
    2019-10-31 09:15:00  235.70  236.50  233.40  234.00
    2019-10-31 11:15:00  234.00  235.75  233.65  235.70
    2019-10-31 13:15:00  235.75  236.15  227.10  227.45
    2019-10-31 15:15:00  227.50  228.00  227.05  227.70
    2019-11-01 09:15:00  227.70  233.05  227.50  232.20
    2019-11-01 11:15:00  232.20  233.50  231.05  231.70
    2019-11-01 13:15:00  231.70  236.45  231.40  235.35
    

    【讨论】:

    • 非常感谢亚历山大!现在我可以用你给出的想法来做到这一点:)
    • @PrakharGupta 很高兴它有帮助。如果您认为此回答最好,请记住选择问题旁边的绿色勾号。谢谢。
    【解决方案2】:

    使用.resample(origin='start') 在您的第一个时间戳开始按 2 小时分组。

    不要通过定义自己的开/高/低/收函数来重新发明轮子。在 pandas 中已经有一个特殊的 .ohlc() 函数 :)

    # import libaries
    import pandas as pd
    from io import StringIO
    
    # example data
    text = """
    TIME                 Open   High    Low     CLOSE   VOLUME
    2019-10-31T09:15:00 235.70  236.50  234.40  234.55  1306585 
    2019-10-31T10:15:00 234.55  235.05  233.40  234.00  765419      
    2019-10-31T11:15:00 234.00  235.25  233.65  234.10  664682      
    2019-10-31T12:15:00 234.10  235.75  234.00  235.70  676993      
    2019-10-31T13:15:00 235.75  236.15  234.25  235.25  527381      
    2019-10-31T14:15:00 235.25  235.80  227.10  227.45  2416364     
    2019-10-31T15:15:00 227.50  228.00  227.05  227.70  613380      
    2019-11-01T09:15:00 227.70  232.45  227.50  231.85  1844851     
    2019-11-01T10:15:00 231.95  233.05  231.05  232.20  1000537     
    2019-11-01T11:15:00 232.20  232.65  231.05  232.60  454966      
    2019-11-01T12:15:00 232.60  233.50  231.40  231.70  569539      
    2019-11-01T13:15:00 231.70  236.45  231.40  235.35  1388397 
    """
    
    # create example df
    df = pd.read_csv(StringIO(text), header=0, sep='\s+', parse_dates=['TIME'])
    
    # melt data to get in the right form for resample and .ohlc()
    df_melt = pd.melt(
        df, 
        id_vars=['TIME'], 
        value_vars=['Open', 'High', 'Low', 'CLOSE']
    ).drop(columns=['variable'])
    
    # resample data by 2 hours and use origin=start to start with 
    # the first available timestamp, in this case 09:15:00
    result_df = (df_melt
        .set_index('TIME')
        .resample('2h', origin='start')
        .ohlc()
        .dropna()
    )
    

    结果数据框:

    TIME                open    high    low     close       
    2019-10-31 09:15:00 235.70  236.50  233.40  234.00
    2019-10-31 11:15:00 234.00  235.75  233.65  235.70
    2019-10-31 13:15:00 235.75  236.15  227.10  227.45
    2019-10-31 15:15:00 227.50  228.00  227.05  227.70
    2019-11-01 09:15:00 227.70  233.05  227.50  232.20
    2019-11-01 11:15:00 232.20  233.50  231.05  231.70
    2019-11-01 13:15:00 231.70  236.45  231.40  235.35
    

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      相关资源
      最近更新 更多