Search code examples
pythonpython-3.xpandasdatetimedate-range

Create minutely date_time range for fixed hours intervals during certain days using Pandas


I have a dataframe with has 900 rows, I need to create a date column from 2021-01-01 to 2021-01-15, for each day, only hours from 10:00:00 to 12:00:00, the interval is 2 minutes,

The expected result will like this: 2021-01-01 10:02:00, 2021-01-01 10:04:00, ..., 2021-01-01 12:00:00, 2021-01-02 10:02:00, ..., 2021-01-02 12:00:00, ..., 2021-01-15 12:00:00.

My trial code:

df['date'] = pd.date_range(datetime(2021, 1, 1, hour=10, minute=2), periods=900, freq='2min')

Out:

DatetimeIndex(['2021-01-01 10:02:00', '2021-01-01 10:04:00',
               '2021-01-01 10:06:00', '2021-01-01 10:08:00',
               '2021-01-01 10:10:00', '2021-01-01 10:12:00',
               '2021-01-01 10:14:00', '2021-01-01 10:16:00',
               '2021-01-01 10:18:00', '2021-01-01 10:20:00',
               ...
               '2021-01-02 15:42:00', '2021-01-02 15:44:00',
               '2021-01-02 15:46:00', '2021-01-02 15:48:00',
               '2021-01-02 15:50:00', '2021-01-02 15:52:00',
               '2021-01-02 15:54:00', '2021-01-02 15:56:00',
               '2021-01-02 15:58:00', '2021-01-02 16:00:00'],
              dtype='datetime64[ns]', length=900, freq='2T')

Obviously, it's not as expected, how could I do that? Thanks.


Solution

  • Instead of specifying the periods= parameter, you can set start= and end= parameters in pd.date_range() and then use .between_time() as follows:

    df = pd.DataFrame({'date':pd.date_range(start=datetime(2021, 1, 1, hour=10), end=datetime(2021, 1, 15, hour=12), freq='2min')})
    df = df.set_index('date')
    date_range = df.between_time('10:02:00', '12:00:00')
    print(date_range)
    
    date
    2021-01-01 10:02:00
    2021-01-01 10:04:00
    2021-01-01 10:06:00
    2021-01-01 10:08:00
    ...
    2021-01-15 11:52:00
    2021-01-15 11:54:00
    2021-01-15 11:56:00
    2021-01-15 11:58:00
    2021-01-15 12:00:00
    900 rows × 0 columns