Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group datetime values by datetime ranges and calculate the min and max values per range

I have the following DatetimeIndex values:

DatetimeIndex(['2021-01-18 01:32:00', '2021-01-18 01:33:00',
               '2021-01-18 01:34:00', '2021-01-18 01:35:00',
               '2021-01-18 01:36:00', '2021-01-18 01:37:00',
               '2021-12-16 12:07:00', '2021-12-16 12:08:00',
               '2021-12-16 12:09:00', '2021-12-16 12:10:00'],
              dtype='datetime64[ns]', length=10, freq=None)

I need to group them by datetime ranges and calculate the min and max values per range.

This is the expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

range  range_min               range_max
1      2021-01-18 01:32:00     2021-01-18 01:37:00
2      2021-12-16 12:07:00     2021-12-16 12:10:00

How can I do it?

I can get min and max across the complete set of values of timestamps, but I don’t know how to group timestamps into ranges.

import numpy as np
import pandas as pd

pd.DataFrame(my_timestamps,columns=["timestamp"]).agg({"timestamp" : [np.min, np.max]})

>Solution :

You can use a custom groupby.agg using the date as grouper with DatetimeIndex.normalize:

idx = pd.DatetimeIndex(['2021-01-18 01:32:00', '2021-01-18 01:33:00',
                        '2021-01-18 01:34:00', '2021-01-18 01:35:00',
                        '2021-01-18 01:36:00', '2021-01-18 01:37:00',
                        '2021-12-16 12:07:00', '2021-12-16 12:08:00',
                        '2021-12-16 12:09:00', '2021-12-16 12:10:00'],)


out = (idx.to_series().groupby(pd.factorize(idx.normalize())[0]+1)
          .agg(**{'range_min': 'min', 'range_max': 'max'})
          .rename_axis('range').reset_index()
      )

print(out)

Output:

   range           range_min           range_max
0      1 2021-01-18 01:32:00 2021-01-18 01:37:00
1      2 2021-12-16 12:07:00 2021-12-16 12:10:00
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading