Search code examples
pythonpandasfrequency

Find periods of time frequency in Pandas dataframe with multiple frequencies


I have multiple Pandas datetime-indexed dataframes, where the index frequency for some dataframes is constant throughout, but for others the frequency switches at some unknown point. For example, the frequency may switch from 5-minute to 1-minute, or from 15-minute to 1-hour.

How can I identify the different frequencies present in the index, and return the start and end times for each?

Here is an example dataframe. Note the switch from 5-minute frequency to 1-minute frequency at 1997-01-01 09:30:00 (magnitude of var_1 happens to change here also, but this is arbitrary).

                        var_1
Datetime                       
1997-01-01 07:00:00     10.28
1997-01-01 07:05:00     11.00
1997-01-01 07:10:00     11.06
1997-01-01 07:15:00     11.06
1997-01-01 07:20:00     10.89
1997-01-01 07:25:00     11.39
1997-01-01 07:30:00     11.78
1997-01-01 07:35:00     11.83
1997-01-01 07:40:00     11.94
1997-01-01 07:45:00     12.06
1997-01-01 07:50:00     12.39
1997-01-01 07:55:00     12.61
1997-01-01 08:00:00     12.33
1997-01-01 08:05:00     12.28
1997-01-01 08:10:00     12.44
1997-01-01 08:15:00     12.50
1997-01-01 08:20:00     12.44
1997-01-01 08:25:00     12.11
1997-01-01 08:30:00     11.78
1997-01-01 08:35:00     11.61
1997-01-01 08:40:00     11.50
1997-01-01 08:45:00     11.56
1997-01-01 08:50:00     11.72
1997-01-01 08:55:00     11.67
1997-01-01 09:00:00     11.56
1997-01-01 09:05:00     11.56
1997-01-01 09:10:00     11.56
1997-01-01 09:15:00     11.11
1997-01-01 09:20:00     11.11
1997-01-01 09:25:00     11.33
1997-01-01 09:30:00      2.63
1997-01-01 09:31:00      2.62
1997-01-01 09:32:00      2.58
1997-01-01 09:33:00      2.62
1997-01-01 09:34:00      2.67
1997-01-01 09:35:00      2.68
1997-01-01 09:36:00      2.65
1997-01-01 09:37:00      2.57
1997-01-01 09:38:00      2.43
1997-01-01 09:39:00      2.31
1997-01-01 09:40:00      2.21
1997-01-01 09:41:00      2.13
1997-01-01 09:42:00      2.04
1997-01-01 09:43:00      1.94
1997-01-01 09:44:00      1.82
1997-01-01 09:45:00      1.75
1997-01-01 09:46:00      1.72
1997-01-01 09:47:00      1.71
1997-01-01 09:48:00      1.64
1997-01-01 09:49:00      1.59
1997-01-01 09:50:00      1.54
1997-01-01 09:51:00      1.47
1997-01-01 09:52:00      1.39
1997-01-01 09:53:00      1.31
1997-01-01 09:54:00      1.24
1997-01-01 09:55:00      1.20
1997-01-01 09:56:00      1.19
1997-01-01 09:57:00      1.18
1997-01-01 09:58:00      1.16
1997-01-01 09:59:00      1.13

Ideally, I would like to return something like:

freq1 = ['1997-01-01 07:00:00', '1997-01-01 09:30:00', '5T']
freq2 = ['1997-01-01 09:30:00', '1997-01-01 09:59:00', '1T']

Where the list items are [start time, end time, frequency].

My goal is take any dataframe with unknown frequency distribution, and return the time periods for each frequency that is present.


Solution

  • Here's one option. Create a Series that increments when the frequency changes, and use that to form groups.

    df = df.reset_index()
    s = df.Datetime.diff().bfill().ne(df.Datetime.diff().shift(-1).ffill()).cumsum()
    
    # Only so the name isn't <lambda>
    def freq(x):
        return pd.infer_freq(x)
    
    freqs = df.groupby(s).Datetime.agg([min, max, freq]).rename_axis(None, 0)
    freqs['max'].update(freqs['min'].shift(-1))  # Fix the end times
    
                      min                 max freq
    0 1997-01-01 07:00:00 1997-01-01 09:30:00   5T
    1 1997-01-01 09:30:00 1997-01-01 09:59:00    T