Search code examples
pythonpandasdatedataframepandasql

How to create a dataframe with date range as values in a column?


I have three variables

csiti - 23454 : (integer)

units - [ 11,22,33,44,55,66,77] : (integer list which is of specific length 'n' always )

begin_date - '2019-10-16' : (string)

How do I create a dataframe from this data like

csiti     units forecast_date
1928422     11    2019-10-16  
1928422     22    2019-10-17  
1928422     33    2019-10-18  
1928422     44    2019-10-19  
1928422     55    2019-10-20  
1928422     66    2019-10-21  
1928422     77    2019-10-22  

The forecast_date column should be future dates starting from begin_date value.


Solution

  • Use DataFrame constructor with date_range for datetimes with periods parameter by length of values in list units:

    csiti = 23454
    units = [11,22,33,44,55,66,77]
    begin_date = '2019-10-16'
    
    df = pd.DataFrame({'csiti':csiti, 
                       'units':units,
                       'forecast_date':pd.date_range(begin_date, periods=len(units))})
    print (df.head(10))
       csiti  units forecast_date
    0  23454     11    2019-10-16
    1  23454     22    2019-10-17
    2  23454     33    2019-10-18
    3  23454     44    2019-10-19
    4  23454     55    2019-10-20
    5  23454     66    2019-10-21
    6  23454     77    2019-10-22