1

I am converting 1 minute candle to 5 minute candle as below:

df = df.resample("5min").agg({
'stock_name': 'first', 
'tr_open': 'first', 
'tr_high': 'max', 
'tr_low': 'min', 
'tr_close': 'last'
})

For some reason, I need the time when OHLC values were taken for this aggregation.

Meaning, tr_time_high should contain the time when the tr_high was HIGH in this 5min candle etc.

How can I achieve this in Pandas?

Thanks in advance.

Sumanth Lingappa
  • 464
  • 1
  • 6
  • 15
  • 1
    If you could show the head of the original dataframe and one example of what you hope to get it would be much easier getting an answer – Marco Nov 28 '21 at 13:04

1 Answers1

1

I prefer used groupby_agg instead resample_agg in your case to allow naming aggregation. Try:

df.groupby(pd.Grouper(freq='5T')).agg(
   stock=('stock_name', 'first'),
   open=('tr_open', 'first'),
   high=('tr_high', 'max'),
   low=('tr_low', 'min'),
   close=('tr_close', 'last'),
   time_high=('tr_high', 'idxmax'),
)
Corralien
  • 109,409
  • 8
  • 28
  • 52