Generate pandas dataframe from a series of start and end dates
<p>I have list of start and end dates which I want to convert into 1 large dataframe.</p>

<p>here is a small reproductible example of what I am trying to acheive</p>

<pre><code>import pandas as pd
from pandas.tseries.offsets import *
import datetime as dt

dates = pd.DataFrame([[dt.datetime(2016,01,01),dt.datetime(2016,02,01)], [dt.datetime(2016,01,10), dt.datetime(2016,02,25)], [dt.datetime(2016,02,10), dt.datetime(2016,03,25)]], columns=['start', 'end'])
</code></pre>

<p>which gives me start and end dates such has:</p>

<pre><code>In[14]: dates
Out[14]: 
       start        end
0 2016-01-01 2016-02-01
1 2016-01-10 2016-02-25
2 2016-02-10 2016-03-25
</code></pre>

<p>I ma trying to create a dataframe with date ranges of weekdays based on those start / end dates and append them together. </p>

<p>this is how I approch the problem but it doesn't feel too much pythonic:</p>

<pre><code>op_series = list()
for row in dates.itertuples():
    time_range = pd.date_range(row.start, row.end, freq=BDay())
    s = len(time_range)
    op_series += (zip(time_range, [row.start]*s, [row.end]*s))

df = pd.DataFrame(op_series, columns=['date', 'start', 'end'])

In[4]: df.head()
Out[4]: 
        date      start        end
0 2016-01-01 2016-01-01 2016-02-01
1 2016-01-04 2016-01-01 2016-02-01
2 2016-01-05 2016-01-01 2016-02-01
3 2016-01-06 2016-01-01 2016-02-01
4 2016-01-07 2016-01-01 2016-02-01
</code></pre>

<p>is there a more efficient way than creating list of data and them gluing them together?</p>

<p>thanks!</p>

<p>Still a bit clumsy, but probably more efficient than yours, as it's all in numpy. Merge a Dataframe with the appropriate day diffs</p>

<pre><code>df = pd.DataFrame([[dt.datetime(2016,1,1),dt.datetime(2016,2,1)], [dt.datetime(2016,1,10), dt.datetime(2016,2,25)], [dt.datetime(2016,2,10), dt.datetime(2016,3,25)]], columns=['start', 'end'])
df['diff'] = (df['end'] - df['start']).dt.days

arr = np.empty(0, dtype=np.uint32)
diff_arr = np.empty(0, dtype=np.uint32)
for value in df['diff'].unique():
    arr = np.append(arr, np.arange(value))
    diff_arr = np.append(diff_arr, np.full(value, value, dtype=np.uint32))
tmp_df = pd.DataFrame(dict(diff=diff_arr, i=arr))
tmp_df['i'] = pd.to_timedelta(tmp_df['i'], unit='D')
df = df.merge(tmp_df, on='diff')
df['date'] = df['start'] + df['i']
df.drop(['i', 'diff'], inplace=True, axis=1)
</code></pre>

python python-2.7 datetime pandas