pcparts
pcparts

Reputation: 41

How to resample the data to 'Odd(instead of even)' 2 hour timeframe?

I have trading logs and would like to resample my data to following.

e.g

9:00  ....
11:00 ....
13:00 ....

I tried to resample my log by using following code, but it will end up with "Even" timeframe.

min_1 = df.resample('2H').ohlc()

Result:

2019-12-12 04:00:00+00:00  7144.0  7165.0  7131.0  7132.5   56757860.0
2019-12-12 06:00:00+00:00  7132.5  7158.5  7132.5  7158.0   44329860.0
2019-12-12 08:00:00+00:00  7158.0  7158.5  7096.5  7121.5  104173650.0
2019-12-12 10:00:00+00:00  7121.5  7223.0  7121.5  7148.5  174419981.0
2019-12-12 12:00:00+00:00  7148.5  7193.5  7148.5  7169.0   65978310.0

Is there a way to resample to "Odd" timeframe?

(The reason why I want to achieve this is because Tradingview 2 hour timeframe is based on odd timeframe so I want to adjust my code to that)

Upvotes: 1

Views: 336

Answers (2)

nithin
nithin

Reputation: 781

You should pass base=1 in parameter here is my solution have a look

 import pandas as pd 
 import numpy as np
 range = pd.date_range('2015-01-01', '2015-12-31', freq='15min')
 df = pd.DataFrame(index = range)

 # Average speed in miles per hour
 df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
 # Distance in miles (speed * 0.5 hours)
 df['distance'] = df['speed'] * 0.25 
 df.resample('2h',base=1).sum()

you can also refer this

Upvotes: 0

pcparts
pcparts

Reputation: 41

Sorry, resolved by myself.

min_1 = df.resample(rule='2H',base=1).ohlc()

(adding "base=1")

Thank you.

Upvotes: 1

Related Questions