Reputation: 11
I am trying to resample OHLC data to 30 mins. The market data starts at at 9:15 and I would like the resampled time to have 9:15-9:45 and so on. But I am able to get the data resampled as 9:00-9:30
Paste Bin link to 1 min market data
pd.DataFrame(download_data).set_index('date'['close'].resample('30T').ohlc()
As you see in the picture the start time is 9:00 and not 9:15...
Upvotes: 1
Views: 3696
Reputation: 453
I will summarize the answers and will add another option for origin.
So yeah, resample method had and argument base
as a starting position, so it was possible to write it like:
data.resample('24h', base=9.5).ohlc()
I am using random data just to show how it is done. The base takes floats, so I assumed 9.5 would be 9:30 and it seems to work.
So I am saying it was possible to use the base argument because it is deprecated already. Although it still brings nice output:
As for now, there is a new, nice and clear argument named origin
in resample method. It takes string as an input, as other answer mentions it also takes keywords like 'start'
or 'end'
but also a string date, so we just put here something something 9:30 and here is what we get:
data.resample('24h', origin='2011-12-31 09:30:00').ohlc()
And here is the output:
So just try it with your data.
Upvotes: 0
Reputation: 11
@TFA the use of origin='start' may not be that useful esp. if our start time does not fit in that rule of starting from 9:15 am, e.g. if we are trying to convert 1 min data to 15 mins, and we give data from 9:37 am, the 15mins candles will start from 9:37 am, which is not what we need.
The better solution that I could find was to use origin = <timestamp>
, so something like origin = datetime.fromisoformat('1970-01-01 09:15:00+05:30')
does the magic.
Overall code:
pd.DataFrame(download_data).set_index('date'['close']).resample('30T', origin=datetime.fromisoformat('1970-01-01 09:15:00+05:30')).ohlc()
Upvotes: 1
Reputation: 81
Though the answers provided here work as expected, it must be noted that both loffset and base are deprecated since version 1.1.0. The best and simplest way now is
pd.DataFrame(download_data).set_index('date'['close']).resample('30T', origin='start').ohlc()
This will set the start time to the first timestamp available in the dataframe.
Upvotes: 0
Reputation: 8521
There is one more way of doing it, you can use the base
argument of resample
:
pd.DataFrame(download_data).set_index('date'['close'].resample('30T', base=15).ohlc()
Upvotes: 3
Reputation: 862791
Solution is add parameter loffset
in resample
:
loffset : timedelta
Adjust the resampled time labels
df = (pd.DataFrame(download_data)
.set_index('date')['close']
.resample('30T', loffset='15min')
.ohlc())
print (df)
open high low close
date
2018-11-05 09:15:00+05:30 25638.25 25641.85 25589.3 25630.00
2018-11-05 09:45:00+05:30 25622.00 25745.00 25622.0 25714.85
2018-11-05 10:15:00+05:30 25720.05 25740.00 25692.9 25717.00
2018-11-05 10:45:00+05:30 25698.30 25744.75 25667.9 25673.95
2018-11-05 11:15:00+05:30 25680.30 25690.45 25642.9 25655.90
Upvotes: 1