Add datetime in pandas dataframe to existing row based on column
<p>I'm new to pandas and am having a problem with this. I have a simple table:</p>

<pre><code>         date  period_number
0  2016-10-26              1
1  2016-10-26              3
2  2016-10-26              5
3  2016-10-27              1
</code></pre>

<p>I want to create 3 equal sized period with start and end times, like this:</p>

<pre><code>         date  period_number               start                end                                   
0  2016-10-26              1    2016-10-26 00:00   2016-10-26 08:00  
1  2016-10-26              3    2016-10-26 08:00   2016-10-26 16:00    
2  2016-10-26              5    2016-10-26 16:00   2016-10-27 00:00                    
3  2016-10-27              1    2016-10-27 00:00   2016-10-27 08:00         
</code></pre>

<p>I tried to do something like this:</p>

<pre><code>for (period, group) in df.groupby('period'):
    if period == 1:
        group['start'] = group['date']
        group['end'] = group['date'] + timedelta(hours=8)
    if period == 3:
        group['start'] = group['date'] + timedelta(hours=8)
        group['end'] = group['date'] + timedelta(hours=16)
    if period == 5:
        group['start'] = group['date'] + timedelta(hours=16)
        group['end'] = group['date'] + timedelta(days=1)
</code></pre>

<p>But I'm getting errors:</p>

<pre><code>A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
</code></pre>

<p>Also tried this but obviously wrong and getting same error:</p>

<pre><code>df[df['period'] == 1]['end'] = df['date'] + timedelta(hours=8)
</code></pre>