Skip to content
Advertisement

How to use pd.data_range() with a frequency of X minutes/hours/seconds?

I need to create a frequent date range with pandas date_range(). This works well with frequency=... parameter. But sometimes my code needs these frequent ranges in longer frequencys. for example 4 Hours or 5 minutes instead of one.

How can I do that with pd.date_range(first_X_datetime, last_X_datetime, freq=frequency)?

If there is not a more efficient way, my idea would be to create a pd.date_range and then drop the unincluded indices. But how to do that ? (currently i cant think of a better approach)

Advertisement

Answer

You can always customize the frequency as you wish instead of only 1 hour or 1 minute.

In [237]: pd.date_range(start, end, freq='2h20min')
Out[237]: 
DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:20:00',
               '2011-01-01 04:40:00', '2011-01-01 07:00:00',
               '2011-01-01 09:20:00', '2011-01-01 11:40:00',
               '2011-01-01 14:00:00', '2011-01-01 16:20:00',
               '2011-01-01 18:40:00', '2011-01-01 21:00:00'],
              dtype='datetime64[ns]', freq='140T')

In [238]: pd.date_range(start, end, freq='1D10U')
Out[238]: 
DatetimeIndex([       '2011-01-01 00:00:00', '2011-01-02 00:00:00.000010',
               '2011-01-03 00:00:00.000020', '2011-01-04 00:00:00.000030',
               '2011-01-05 00:00:00.000040', '2011-01-06 00:00:00.000050',
               '2011-01-07 00:00:00.000060', '2011-01-08 00:00:00.000070',
               '2011-01-09 00:00:00.000080', '2011-01-10 00:00:00.000090'],
              dtype='datetime64[ns]', freq='86400000010U')

Please check it out here for more information:

https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement