Append with date_range() in Python
Question: I have a csv file which contains start dates and end dates, with format
`dd/mm/yy`. These are read by :

    
    
    dateparse = lambda x: pnd.datetime.strptime(x, '%d/%m/%y')
    df = pnd.read_csv('file.csv',sep=';',parse_dates=['StartDate','EndDate'], date_parser=dateparse)
    

A sample of the dataframe looks like this:

    
    
        StartDate   EndDate
    0   2015-07-15  2015-07-18
    1   2015-06-06  2015-06-08
    

I want to get all the dates listed in these intervals in a column in a new
dataframe:

    
    
        Date
    0   2015-07-15
    1   2015-07-16
    2   2015-07-17
    3   2015-07-18
    4   2015-06-06  
    5   2015-06-07  
    6   2015-06-08
    

I use iteratively `date_range(StartDate, EndDate)`, appending each time the
result, but I get either an empty array, or something like

    
    
    [[2015-07-15, 2015-07-16, 2015-07-17, 2015-07-18], [ 2015-06-06, 2015-06-07 , 2015-06-08 ]]
    

and I would like

    
    
    [ 2015-07-15, 2015-07-16, 2015-07-17, 2015-07-18, 2015-06-06, 2015-06-07 , 2015-06-08 ]
    

What to do?


Answer: You can chain the ranges together using `itertools.chain` to create your list
of dates:

    
    
    from itertools import chain
    new_df = pnd.DataFrame(list(chain.from_iterable(pnd.date_range(r["StartDate"],r["EndDate"]) 
             for _,r in df.iterrows())), columns=("Date",))
    

Output:

    
    
            Date
    0 2015-07-15
    1 2015-07-16
    2 2015-07-17
    3 2015-07-18
    4 2015-06-06
    5 2015-06-07
    6 2015-06-08
    

