0

I have a simple OHLCV time-series. I obtain it from yahoo finance.

                                   Open          High     Adj Close     Volume
Datetime                                                                        
2021-11-27 00:00:00+00:00  53736.429688  54287.300781  54287.300781  349732864
2021-11-27 00:15:00+00:00  54321.816406  54470.097656  54470.097656   50278400
2021-11-27 00:30:00+00:00  54362.085938  54688.476562  54563.937500  125132800
2021-11-27 00:45:00+00:00  54552.707031  54552.707031  54208.027344   23285760
2021-11-27 01:00:00+00:00  54186.679688  54304.398438  54080.007812          0
2022-01-25 07:30:00+00:00  35861.457031  36036.191406  36023.011719  389357568
2022-01-25 07:45:00+00:00  36036.500000  36078.332031  36075.312500  102707200
2022-01-25 08:00:00+00:00  36069.089844  36211.867188  36152.500000  234246144
2022-01-25 08:15:00+00:00  36179.812500  36179.812500  36179.812500  125779968
2022-01-25 08:16:00+00:00  36283.058594  36283.058594  36283.058594          0

I know how to group them by frequency using resample.

df.resample("6H", origin="end")

Also I use origin="end" so that the intervals are relative to the end.

Can I somehow group the time-series by some custom defined range. Let's say: 6horus, 12h, 24h, 7days, 1month, 3m , 1y ... Those are the time-periods substracted from the last value.

The date-range would look like this:

2021-01-25 08:16:00+00:00 
2021-10-25 08:16:00+00:00
2021-12-25 08:16:00+00:00
2022-01-18 08:16:00+00:00
2022-01-24 08:16:00+00:00
2022-01-24 20:16:00+00:00 
2022-01-25 02:16:00+00:00 
2022-01-25 08:16:00+00:00

Now I know I could do several resamples and than filter and concatenate the resulting data-frames. I was wondering is there a simpler way to do this?

Borut Flis
  • 15,715
  • 30
  • 92
  • 119
  • Could you please add some example of your desirable output? I am not sure if I understand correctly what are you trying to achieve. – Daniel Wlazło Jan 25 '22 at 09:52
  • Have you converted your date time objects to datetime using pandas (required to perform operations on them) your_df["date"] = pd.to_datetime(your_df['date']) – Jeremy Savage Jan 25 '22 at 09:56
  • @DanielWlazło I added an example of the date range on which I would like to perform aggregate operations. – Borut Flis Jan 26 '22 at 08:34
  • @JeremySavage Yes, the index is datetime, that is why I can call resample. – Borut Flis Jan 26 '22 at 08:35

0 Answers0