Ben Chuanlong Du's Blog

It is never too late to learn.

Create a Date Range in Python

pandas.date_range

In [1]:
import pandas as pd
import datetime
In [3]:
pd.date_range("2018-01-01", "2018-01-10")
Out[3]:
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'],
              dtype='datetime64[ns]', freq='D')
In [3]:
t1 = datetime.datetime.now()
t2 = datetime.datetime.now() + datetime.timedelta(days=10)
pd.date_range(t1, t2)
Out[3]:
DatetimeIndex(['2020-11-01 19:58:04.158545', '2020-11-02 19:58:04.158545',
               '2020-11-03 19:58:04.158545', '2020-11-04 19:58:04.158545',
               '2020-11-05 19:58:04.158545', '2020-11-06 19:58:04.158545',
               '2020-11-07 19:58:04.158545', '2020-11-08 19:58:04.158545',
               '2020-11-09 19:58:04.158545', '2020-11-10 19:58:04.158545',
               '2020-11-11 19:58:04.158545'],
              dtype='datetime64[ns]', freq='D')
In [ ]:
 

Comments