Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

Pandas Datetime: Exercise-14 with Solution

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?



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m