Search code examples
pythonpandasohlc

How do i convert m1 OHLC data to m15 or any timeframe OHLC in pandas?


I'm trying to convert M1 OHLC data to M15 using the pandas resample function but not getting anywhere, this is what I've got:

df = pd.read_csv("EURUSD.csv")
df = df.set_index("DatetimeIndex")
print("\tDone!")

df = df.resample('1H').agg({'Open': 'first', 
                            'High': 'max', 
                            'Low': 'min', 
                            'Close': 'last'})

I get the error

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

I tried removing the .set_index and it gave another error saying can't do RangeIndex

the data I have looks like this:

m1 data


Solution

  • My strong guess is that the problem is with the data type of your index. CSV files do not store data types and pandas has to do some guesswork in finding the types.

    Since pandas couldn't figure out the first column had datetime values, it considered it a string; So when you set it as the index it created a vanilla index for you (hence the Index type in the error, not a DatetimeIndex) You want to convert it to the proper format while/after the read_csv call and before setting it as the index.

    Please refer to this answer to find out about how to convert the format after reading the CSV file. If you wanna do the data conversion while reading the CSV (which is nicer and takes less time/memory) take a look at the docs from where the parse_dates parameter is downwards.

    P.S: A tip for asking better questions, you can share a part of your data like this to provide a minimal reproducible example:

    from io import StringIO
    df = pandas.read_csv(StringIO("""A,B,C
                                     a,b,c
                                     d,,f"""))
    

    often times you might find the answer to your question while trying to make the minimal example before getting on SO!