Python Pandas - Generate dates in a range


To generate dates in a range, use the date _range() method. At first, import the required pandas library with an alias −

import pandas as pd

Now, let’s say you need to generate dates in arrange, therefore for this, mention the date from where you want to begin. Here, we have mentioned 1st June 2021 and period of 60 days −

dates = pd.date_range('6/1/2021', periods=60)

Example

Following is the complete code −

import pandas as pd

# generate dates in a range
# period is 60 i.e. 60 days from 1st June 2021
dates = pd.date_range('6/1/2021', periods=60)

print"Displaying dates in a range...\n",dates

Output

This will produce the following output −

Displaying dates in a range...
   DatetimeIndex(['2021-06-01', '2021-06-02', '2021-06-03', '2021-06-04',
                  '2021-06-05', '2021-06-06', '2021-06-07', '2021-06-08',
                  '2021-06-09', '2021-06-10', '2021-06-11', '2021-06-12',
                  '2021-06-13', '2021-06-14', '2021-06-15', '2021-06-16',
                  '2021-06-17', '2021-06-18', '2021-06-19', '2021-06-20',
                  '2021-06-21', '2021-06-22', '2021-06-23', '2021-06-24',
                  '2021-06-25', '2021-06-26', '2021-06-27', '2021-06-28',
                  '2021-06-29', '2021-06-30', '2021-07-01', '2021-07-02',
                  '2021-07-03', '2021-07-04', '2021-07-05', '2021-07-06',
                  '2021-07-07', '2021-07-08', '2021-07-09', '2021-07-10',
                  '2021-07-11', '2021-07-12', '2021-07-13', '2021-07-14',
                  '2021-07-15', '2021-07-16', '2021-07-17', '2021-07-18',
                  '2021-07-19', '2021-07-20', '2021-07-21', '2021-07-22',
                  '2021-07-23', '2021-07-24', '2021-07-25', '2021-07-26',
                  '2021-07-27', '2021-07-28', '2021-07-29', '2021-07-30'],
                 dtype='datetime64[ns]', freq='D')

Updated on: 16-Sep-2021

542 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements