Open In App

Python | pandas.date_range() method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier.

pandas.date_range() is one of the general functions in Pandas which is used to return a fixed frequency DatetimeIndex.

Pandas.date_range() Syntax

Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)

Parameters:

start : Left bound for generating dates.

end : Right bound for generating dates.

periods : Number of periods to generate.

freq : Frequency strings can have multiples, e.g. ‘5H’. See here for a list of frequency aliases.

tz : Time zone name for returning localized DatetimeIndex. By default, the resulting DatetimeIndex is timezone-naive.

normalize : Normalize start/end dates to midnight before generating date range.

name : Name of the resulting DatetimeIndex.

closed : Make the interval closed with respect to the given frequency to the ‘left’, ‘right’, or both sides (None, the default).

Returns: DatetimeIndex

Date_range() in Pandas Examples

The date_range() function in Pandas for generating sequences of dates. It allows you to specify the starting date, ending date, frequency, and timezone for the generated dates, making it versatile for various time-based applications. We may build date ranges in a number of ways using the pandas.date_range function, a few of which are as follows:

Specifying the Start and End

The code will provide a range of dates with a by-default frequency from start to finish.

Python3




# importing pandas as pd
import pandas as pd
 
per1 = pd.date_range(start ='1-1-2018',
         end ='1-05-2018', freq ='5H')
 
for val in per1:
    print(val)


Output:

2018-01-01 00:00:00
2018-01-01 05:00:00
2018-01-01 10:00:00
2018-01-01 15:00:00
2018-01-01 20:00:00
2018-01-02 01:00:00
2018-01-02 06:00:00
2018-01-02 11:00:00
2018-01-02 16:00:00
2018-01-02 21:00:00
2018-01-03 02:00:00
2018-01-03 07:00:00
2018-01-03 12:00:00
2018-01-03 17:00:00
2018-01-03 22:00:00
2018-01-04 03:00:00
2018-01-04 08:00:00
2018-01-04 13:00:00
2018-01-04 18:00:00
2018-01-04 23:00:00


Specify Start, End and Frequency

The code will provide a range of dates with a given frequency from start to finish.

Python3




# importing pandas as pd
import pandas as pd
 
dRan1 = pd.date_range(start ='1-1-2018',
           end ='8-01-2018', freq ='M')
 
dRan2 = pd.date_range(start ='1-1-2018',
         end ='11-01-2018', freq ='3M')
 
print(dRan1, '\n\n', dRan2)


Output:

DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',
               '2018-05-31', '2018-06-30', '2018-07-31'],
              dtype='datetime64[ns]', freq='M') 
 DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31'], dtype='datetime64[ns]', freq='3M')


Specifying Start and Periods

The code will provide a range of dates with a predetermined number of periods starting at the beginning.

Python3




# importing pandas as pd
import pandas as pd
   
# Specify start and periods, the number of periods (days).
dRan1 = pd.date_range(start ='1-1-2018', periods = 13)
   
# Specify end and periods, the number of periods (days).
dRan2 = pd.date_range(end ='1-1-2018', periods = 13)
   
# Specify start, end, and periods; the frequency 
# is generated automatically (linearly spaced).
dRan3 = pd.date_range(start ='01-03-2017'
            end ='1-1-2018', periods = 13)
   
print(dRan1, "\n\n", dRan2, '\n\n', dRan3)


Output:

DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
               '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08',
               '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12',
               '2018-01-13'],
              dtype='datetime64[ns]', freq='D') 
 DatetimeIndex(['2017-12-20', '2017-12-21', '2017-12-22', '2017-12-23',
               '2017-12-24', '2017-12-25', '2017-12-26', '2017-12-27',
               '2017-12-28', '2017-12-29', '2017-12-30', '2017-12-31',
               '2018-01-01'],
              dtype='datetime64[ns]', freq='D') 
 DatetimeIndex(['2017-01-03 00:00:00', '2017-02-02 06:00:00',
               '2017-03-04 12:00:00', '2017-04-03 18:00:00',
               '2017-05-04 00:00:00', '2017-06-03 06:00:00',
               '2017-07-03 12:00:00', '2017-08-02 18:00:00',
               '2017-09-02 00:00:00', '2017-10-02 06:00:00',
               '2017-11-01 12:00:00', '2017-12-01 18:00:00',
               '2018-01-01 00:00:00'],
              dtype='datetime64[ns]', freq=None)


Specifying Start, Periods and tz

The code will provide a range of dates starting at the beginning that have a predetermined number of periods and the timezone tz.

Python3




# importing pandas as pd
import pandas as pd
 
# Specify start and periods, the number of periods (days).
dRan1 = pd.date_range(start='1-1-2018', periods=13, tz='Asia/Tokyo')
 
dRan1


Output:

DatetimeIndex(['2018-01-01 00:00:00+09:00', '2018-01-02 00:00:00+09:00',
               '2018-01-03 00:00:00+09:00', '2018-01-04 00:00:00+09:00',
               '2018-01-05 00:00:00+09:00', '2018-01-06 00:00:00+09:00',
               '2018-01-07 00:00:00+09:00', '2018-01-08 00:00:00+09:00',
               '2018-01-09 00:00:00+09:00', '2018-01-10 00:00:00+09:00',
               '2018-01-11 00:00:00+09:00', '2018-01-12 00:00:00+09:00',
               '2018-01-13 00:00:00+09:00'],
              dtype='datetime64[ns, Asia/Tokyo]', freq='D')




Last Updated : 04 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads