w3resource

Pandas Datetime: Generate sequences of fixed-frequency dates and time spans


Write a Pandas program to generate sequences of fixed-frequency dates and time spans.

Sample Solution :

Python Code :

import pandas as pd
dtr = pd.date_range('2018-01-01', periods=12, freq='H')
print("Hourly frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='min')
print("\nMinutely frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='S')
print("\nSecondly frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='2H')
print("nMultiple Hourly frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='5min')
print("\nMultiple Minutely frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='BQ')
print("\nMultiple Secondly frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='w')
print("\nWeekly frequency:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='2h20min')
print("\nCombine together day and intraday offsets-1:")
print(dtr)
dtr = pd.date_range('2018-01-01', periods=12, freq='1D10U')
print("\nCombine together day and intraday offsets-2:")
print(dtr)

Sample Output:

Hourly frequency:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 01:00:00',
               '2018-01-01 02:00:00', '2018-01-01 03:00:00',
               '2018-01-01 04:00:00', '2018-01-01 05:00:00',
               '2018-01-01 06:00:00', '2018-01-01 07:00:00',
               '2018-01-01 08:00:00', '2018-01-01 09:00:00',
               '2018-01-01 10:00:00', '2018-01-01 11:00:00'],
              dtype='datetime64[ns]', freq='H')

Minutely frequency:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 00:01:00',
               '2018-01-01 00:02:00', '2018-01-01 00:03:00',
               '2018-01-01 00:04:00', '2018-01-01 00:05:00',
               '2018-01-01 00:06:00', '2018-01-01 00:07:00',
               '2018-01-01 00:08:00', '2018-01-01 00:09:00',
               '2018-01-01 00:10:00', '2018-01-01 00:11:00'],
              dtype='datetime64[ns]', freq='T')

Secondly frequency:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 00:00:01',
               '2018-01-01 00:00:02', '2018-01-01 00:00:03',
               '2018-01-01 00:00:04', '2018-01-01 00:00:05',
               '2018-01-01 00:00:06', '2018-01-01 00:00:07',
               '2018-01-01 00:00:08', '2018-01-01 00:00:09',
               '2018-01-01 00:00:10', '2018-01-01 00:00:11'],
              dtype='datetime64[ns]', freq='S')
nMultiple Hourly frequency:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 02:00:00',
               '2018-01-01 04:00:00', '2018-01-01 06:00:00',
               '2018-01-01 08:00:00', '2018-01-01 10:00:00',
               '2018-01-01 12:00:00', '2018-01-01 14:00:00',
               '2018-01-01 16:00:00', '2018-01-01 18:00:00',
               '2018-01-01 20:00:00', '2018-01-01 22:00:00'],
              dtype='datetime64[ns]', freq='2H')

Multiple Minutely frequency:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 00:05:00',
               '2018-01-01 00:10:00', '2018-01-01 00:15:00',
               '2018-01-01 00:20:00', '2018-01-01 00:25:00',
               '2018-01-01 00:30:00', '2018-01-01 00:35:00',
               '2018-01-01 00:40:00', '2018-01-01 00:45:00',
               '2018-01-01 00:50:00', '2018-01-01 00:55:00'],
              dtype='datetime64[ns]', freq='5T')

Multiple Secondly frequency:
DatetimeIndex(['2018-03-30', '2018-06-29', '2018-09-28', '2018-12-31',
               '2019-03-29', '2019-06-28', '2019-09-30', '2019-12-31',
               '2020-03-31', '2020-06-30', '2020-09-30', '2020-12-31'],
              dtype='datetime64[ns]', freq='BQ-DEC')

Weekly frequency:
DatetimeIndex(['2018-01-07', '2018-01-14', '2018-01-21', '2018-01-28',
               '2018-02-04', '2018-02-11', '2018-02-18', '2018-02-25',
               '2018-03-04', '2018-03-11', '2018-03-18', '2018-03-25'],
              dtype='datetime64[ns]', freq='W-SUN')

Combine together day and intraday offsets-1:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 02:20:00',
               '2018-01-01 04:40:00', '2018-01-01 07:00:00',
               '2018-01-01 09:20:00', '2018-01-01 11:40:00',
               '2018-01-01 14:00:00', '2018-01-01 16:20:00',
               '2018-01-01 18:40:00', '2018-01-01 21:00:00',
               '2018-01-01 23:20:00', '2018-01-02 01:40:00'],
              dtype='datetime64[ns]', freq='140T')

Combine together day and intraday offsets-2:
DatetimeIndex([       '2018-01-01 00:00:00', '2018-01-02 00:00:00.000010',
               '2018-01-03 00:00:00.000020', '2018-01-04 00:00:00.000030',
               '2018-01-05 00:00:00.000040', '2018-01-06 00:00:00.000050',
               '2018-01-07 00:00:00.000060', '2018-01-08 00:00:00.000070',
               '2018-01-09 00:00:00.000080', '2018-01-10 00:00:00.000090',
               '2018-01-11 00:00:00.000100', '2018-01-12 00:00:00.000110'],
              dtype='datetime64[ns]', freq='86400000010U')

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to add 100 days with reporting date of unidentified flying object (UFO).
Next: Write a Pandas program to create a conversion between strings and datetime.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.