DataFrame.date_range()

Pandas date & time Pandas

Generate Datetimeindex by using the frequency option.
import pandas as pd 
pd.date_range(start='4/20/2020', end='4/27/2020')
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')

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')

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()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer