Pandas data_range Function

Generating A Series Of Dates

Luckily Pandas has a function named date-range to generate a series of dates or times. We will see how we can use it to solve some problems that we may encounter at work. Here, we will solve a few questions.

import pandas as pd
pd.date_range(start = '1/1/2020', end='1/15/2020')#Output:
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'], dtype='datetime64[ns]', freq='D')
pd.date_range('1/1/2020', '1/15/2020', freq='2D')#Output:
DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05', '2020-01-07', '2020-01-09', '2020-01-11', '2020-01-13', '2020-01-15'], dtype='datetime64[ns]', freq='2D')
pd.date_range('1/1/2020', '1/15/2020', freq='B')#Output:
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10', '2020-01-13', '2020-01-14', '2020-01-15'], dtype='datetime64[ns]', freq='B')
pd.date_range('1/1/2020', periods = 9, freq='B')#Output:
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10', '2020-01-13'], dtype='datetime64[ns]', freq='B')
pd.date_range(end='1/1/2020', periods = 5)#Output:
DatetimeIndex(['2019-12-28', '2019-12-29', '2019-12-30', '2019-12-31', '2020-01-01'], dtype='datetime64[ns]', freq='D')
pd.date_range('1/1/2020', periods = 5, freq='3M')#Output:
DatetimeIndex(['2020-01-31', '2020-04-30', '2020-07-31', '2020-10-31', '2021-01-31'], dtype='datetime64[ns]', freq='3M')
Image for post
pd.DataFrame(pd.date_range('1/1/2018', periods=12, freq='BM'), columns=['Date'])
Image for post
pd.date_range('1/11/2019', periods=5, tz='Africa/Abidjan', freq='3H')#Output: 
DatetimeIndex(['2019-01-11 00:00:00+00:00', '2019-01-11 03:00:00+00:00', '2019-01-11 06:00:00+00:00', '2019-01-11 09:00:00+00:00', '2019-01-11 12:00:00+00:00'], dtype='datetime64[ns, Africa/Abidjan]', freq='3H')

 

#python #dataScience #TimeSeries #DataAnalysis

Leave a Reply

Close Menu