w3resource

Pandas: Date range using a startpoint date and a number of periods


8. Date Range Generation

Write a Pandas program to create a date range using a startpoint date and a number of periods.

Sample Solution:

Python Code :

import pandas as pd
date_range = pd.date_range('2020-01-01', periods=45)
print("Date range of perods 45:")
print(date_range)

Sample Output:

Date range of perods 45:
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
               '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
               '2020-01-09', '2020-01-10', '2020-01-11', '2020-01-12',
               '2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16',
               '2020-01-17', '2020-01-18', '2020-01-19', '2020-01-20',
               '2020-01-21', '2020-01-22', '2020-01-23', '2020-01-24',
               '2020-01-25', '2020-01-26', '2020-01-27', '2020-01-28',
               '2020-01-29', '2020-01-30', '2020-01-31', '2020-02-01',
               '2020-02-02', '2020-02-03', '2020-02-04', '2020-02-05',
               '2020-02-06', '2020-02-07', '2020-02-08', '2020-02-09',
               '2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13',
               '2020-02-14'],
              dtype='datetime64[ns]', freq='D')

For more Practice: Solve these Related Problems:

  • Write a Pandas program to generate a date range starting from a specific date with a fixed number of periods, skipping weekends.
  • Write a Pandas program to create a date range with a custom frequency (e.g., every 3 days) and then visualize the dates.
  • Write a Pandas program to generate a date range with irregular intervals by randomly dropping dates from a complete sequence.
  • Write a Pandas program to create a date range from a start date and period count, then display the first and last dates.

Python Code Editor:

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

Previous: Write a Pandas program to create a time series object that has time indexed data. Also select the dates of same year and select the dates between certain dates.
Next: Write a Pandas program to create a whole month of dates in daily frequencies. Also find the maximum, minimum timestamp and indexs.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.