0

I want to resample a pandas time series counting backwards. For example, let's set up a simple time series of 11 days:

index = pd.date_range('01-01-2018 00:00:00', '01-11-2018 23:59:00', freq='1T')
randint = np.random.randint(low=0, high=9, size=(len(index), 1))

df = pd.DataFrame(randint, index=index, columns=['random'])
print(df.tail(100))
df.resample('5T').ohlc()

gives me the following results

2018-01-11 23:35:00       8
2018-01-11 23:36:00       5
2018-01-11 23:37:00       4
2018-01-11 23:38:00       2
2018-01-11 23:39:00       4
2018-01-11 23:40:00       2
2018-01-11 23:41:00       0
2018-01-11 23:42:00       4
2018-01-11 23:43:00       5
2018-01-11 23:44:00       4
2018-01-11 23:45:00       2
2018-01-11 23:46:00       1
2018-01-11 23:47:00       3
2018-01-11 23:48:00       8
2018-01-11 23:49:00       4
2018-01-11 23:50:00       0
2018-01-11 23:51:00       4
2018-01-11 23:52:00       3
2018-01-11 23:53:00       8
2018-01-11 23:54:00       3
2018-01-11 23:55:00       5
2018-01-11 23:56:00       1
2018-01-11 23:57:00       1
2018-01-11 23:58:00       1
2018-01-11 23:59:00       7

                     open   high    low close
2018-01-01 00:00:00 3   6   3   4
2018-01-01 00:05:00 3   8   3   8
2018-01-01 00:10:00 3   8   0   0
2018-01-01 00:15:00 3   8   3   8
2018-01-01 00:20:00 5   8   0   0
... ... ... ... ...
2018-01-11 23:35:00 8   8   2   4
2018-01-11 23:40:00 2   5   0   4
2018-01-11 23:45:00 2   8   1   4
2018-01-11 23:50:00 0   8   0   3
2018-01-11 23:55:00 5   7   1   7

but what I want is like

                     open   high    low close
... ... ... ... ...
2018-01-11 23:39:00 4   5   0   5
2018-01-11 23:44:00 4   8   1   8
2018-01-11 23:49:00 4   8   0   1
2018-01-11 23:57:00 8   5   1   1
2018-01-11 23:54:00 3   5   1   1
2018-01-11 23:59:00 5   7   1   7

How can I resample my dataframe as this?

I googled for a while but couldn't find the answer.

jad
  • 29
  • 3
  • This question is similar, does it help: [Pandas resample time series counting backwards (or reverse resample)](https://stackoverflow.com/q/51787945/1609514) – Bill Feb 22 '23 at 04:05
  • Can you please explain the ordering of the times in your desired output: `[..., 23:49:00, 23:57:00, 23:54:00, 23:59:00]` – Bill Feb 22 '23 at 05:03

1 Answers1

-1

df.resample('5min', origin='end').ohlc() solved my problem.

jad
  • 29
  • 3