Python Pandas Date & Time
Generate Datetimeindex by using the frequency option. ( List of all Frequency Aliases )
import pandas as pd
df=pd.date_range(start='4/20/2020', end='4/27/2020')
print(df)
Default frequency is D ( Day ) so we will get all days starting from 20th April 2020 to 27th April 2020.
DatetimeIndex(['2020-04-20', '2020-04-21', '2020-04-22', '2020-04-23',
'2020-04-24', '2020-04-25', '2020-04-26', '2020-04-27'],
dtype='datetime64[ns]', freq='D')
Using today's date
from datetime import timedelta
import pandas as pd
dt_now=pd.to_datetime('now') # todays date
dt_previous=(dt_now-timedelta(days=10)).strftime("%Y-%m-%d")
df=pd.date_range(start=dt_previous, end=dt_now) # last 10 days
print(df)
Creating a list by using tolist()
import datetime
import pandas as pd
l1=pd.date_range(end=datetime.date.today(),periods=10).tolist()
print(l1)
Options
We used start and end options in above code. Similarly we can use other options.
periods
We can specify integer values like periods=3 , this is optional.
import pandas as pd
pd.date_range(start='4/20/2020', end='4/27/2020',periods=3)
Output
DatetimeIndex(['2020-04-20 00:00:00', '2020-04-23 12:00:00',
'2020-04-27 00:00:00'],
dtype='datetime64[ns]', freq=None)
freq
Default value is D . We can specify what to consider as frequence of generation of elements. Here D is for Days, M for Month, H for hours etc.
3D6H : 3 Days and 6 Hours
3H30min : 3 Hours 30 Minutes
pd.date_range(start='4/20/2020', end='4/27/2020',freq='D')
Output
DatetimeIndex(['2020-04-20', '2020-04-21', '2020-04-22', '2020-04-23',
'2020-04-24', '2020-04-25', '2020-04-26', '2020-04-27'],
dtype='datetime64[ns]', freq='D')
Using Hour as frequency we can specify periods . But here we will remove the end option.
import pandas as pd
pd.date_range(start='4/20/2020', freq='H',periods=5)
Output
DatetimeIndex(['2020-04-20 00:00:00', '2020-04-20 01:00:00',
'2020-04-20 02:00:00', '2020-04-20 03:00:00',
'2020-04-20 04:00:00'],
dtype='datetime64[ns]', freq='H')
pd.date_range(start='4/20/2020', freq='5h30min',periods=5)
Output
DatetimeIndex(['2020-04-20 00:00:00', '2020-04-20 05:30:00',
'2020-04-20 11:00:00', '2020-04-20 16:30:00',
'2020-04-20 22:00:00'],
dtype='datetime64[ns]', freq='330T')
pd.date_range(start='4/20/2010', end='4/27/2020',freq='A-FEB') # Feb End
DatetimeIndex(['2011-02-28', '2012-02-29', '2013-02-28', '2014-02-28',
'2015-02-28', '2016-02-29', '2017-02-28', '2018-02-28',
'2019-02-28', '2020-02-29'],
dtype='datetime64[ns]', freq='A-FEB')
Month start
pd.date_range(start='1/1/2010', periods=12,freq='MS') # month start
Output
DatetimeIndex(['2010-01-01', '2010-02-01', '2010-03-01', '2010-04-01',
'2010-05-01', '2010-06-01', '2010-07-01', '2010-08-01',
'2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01'],
dtype='datetime64[ns]', freq='MS')
Month end
pd.date_range(start='1/1/2010', periods=12,freq='M') # month end
Output
DatetimeIndex(['2010-01-31', '2010-02-28', '2010-03-31', '2010-04-30',
'2010-05-31', '2010-06-30', '2010-07-31', '2010-08-31',
'2010-09-30', '2010-10-31', '2010-11-30', '2010-12-31'],
dtype='datetime64[ns]', freq='M')
Hour and Minutes
All hours of a date
df=pd.date_range(start='4/20/2022', end='4/21/2020',freq='H') # all hours
df=pd.date_range(start='4/20/2022', freq='H',periods=5) #first 5 Hour
df=pd.date_range(start='4/20/2022', freq='T',periods=5) # minutes
Using present date and time we can get next 5 hours from now.
df=pd.date_range(start=pd.to_datetime('now'), freq='H',periods=5)
Output
DatetimeIndex(['2022-06-10 13:00:26.938739', '2022-06-10 14:00:26.938739',
'2022-06-10 15:00:26.938739', '2022-06-10 16:00:26.938739',
'2022-06-10 17:00:26.938739'],
dtype='datetime64[ns]', freq='H')
Using python date and time
from datetime import datetime
my_now = datetime.now()
df=pd.date_range(start=my_now, freq='T',periods=5)
tz
Time zone we can add. tz='Asia/Kolkata'
import pandas as pd
pd.date_range(start='1/1/2010', periods=3,freq='4M' ,tz='Asia/Kolkata')
Output
DatetimeIndex(['2010-01-31 00:00:00+05:30', '2010-05-31 00:00:00+05:30',
'2010-09-30 00:00:00+05:30'],
dtype='datetime64[ns, Asia/Kolkata]', freq='4M')
normalize
Normalize the start and end dates to midnight before generating the date range.
name
We can give name of to the resulting DataFrame.
closed
Make the interval close to left ( start ) or right ( end ), the default value is None
closed='right'
pd.date_range(start='1/1/2010', end='1/5/2010',closed='right')
Output
DatetimeIndex(['2010-01-02', '2010-01-03', '2010-01-04', '2010-01-05'])
Let us change it to closed='left'
pd.date_range(start='1/1/2010', end='1/5/2010',closed='left')
output
DatetimeIndex(['2010-01-01', '2010-01-02', '2010-01-03', '2010-01-04'])
« Pandas date & time
to_datetime()
period_range()
strftime()
← Subscribe to our YouTube Channel here