Group DataFrame in 5-minute intervals

前端 未结 3 1920
忘了有多久
忘了有多久 2020-12-10 20:13

How do I get just the 5 minute data using Python/pandas out of this csv? For every 5 minute interval I\'m trying to get the DATE, TIME,OPEN, HIGH, LOW, CLOSE, VOLUME for tha

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 20:56

    Another way using pandas is to use its TimeGrouper-function. Its purpose is just meant for use cases like yours.

    import pandas as pd
    
    df = pd.DataFrame("Your data provided above")
    df["DATE"] = pd.to_datetime(df["DATE"])
    df.set_index("DATE", inplace=True)
    
    df = df.groupby(pd.TimeGrouper('5Min')).agg({
                                            "OPEN":  "first",
                                            "HIGH":  "max",
                                            "LOW":   "min",
                                            "CLOSE": "last",
                                            "VOLUME": "sum"
                                        })
    

    The provided script uses an aggregation you might have in mind since you're dealing with stock-data. It aggregates in a way that you will end up with the 5-min candles resulting from your 1-min candles.

提交回复
热议问题