title: How add timestamp between two specific timestamp of an industrial equipment - Python Pandas
body: <p>I'm working on the availability of a specific equipment. After several data processing I finally have the time in hours of the unavailability of the equipment.</p>
  
  <p>By analyzing my results, I find that I have an unavailability of 41 hours in a day ... impossible.</p>
  
  <p>I finally found the problem, here are two examples:</p>
  
  <pre><code>Date              Unavailability
  2019-10-25                 41.47
  
  Flag                       Timestamp
  begin         2019-10-25 16:35:22.48
  end           2019-10-27 09:50:31.71
  begin         2019-10-29 14:04:33.09
  end           2019-10-29 14:05:07.63
  </code></pre>
  
  <p>In example 1, I have a begin which starts on 10-25 but which ends on 27 ... while the company has closed at 5pm so they no longer produce. But the problem was not resolved before 27 at 9:50 a.m.</p>
  
  <p>I found a solution but I still cruelly lack experience on the conditions of timestamps in pandas. </p>
  
  <p>The business opens at 7:00 a.m. each day and closes at 5:00 p.m</p>
  
  <p>The line with the "end  2019-10-27" flag <strong>does not have the same date as the line before</strong> the "begin 2019-10-25". To have the sequence begin-end with the same date,  so we must add:</p>
  
  <pre><code>Flag                   Timestamp
  end       2019-10-25 17:00:00.00
  begin     2019-10-27 07:00:00.00
  </code></pre>
  
  <p>We must add day(s) <strong>ONLY</strong> If the sequence begin-end are of different date. So we make the difference of the days, 27-25 = 2 and to complete the unavailability it takes the date between the begin and the end.</p>
  
  <p><strong>no insertion</strong> between end-begin sequences even if there are several days. between</p>
  
  <pre><code>Flag                   Timestamp
  begin     2019-10-26 07:00:00.00
  end       2019-10-26 17:00:00.00
  </code></pre>
  
  <p>If we go back to example 1 with concatenate, this gives:</p>
  
  <pre><code>Date              Unavailability
  2019-10-25                 25min
  2019-10-26                   10h 
  2019-10-27               2h50min 
  
  Flag                     Timestamp
  begin       2019-10-25 16:35:22.48
  end         2019-10-25 17:00:00.00
  begin       2019-10-26 07:00:00.00
  end         2019-10-26 17:00:00.00
  begin       2019-10-27 07:00:00.00
  end         2019-10-27 09:50:31.71
  begin       2019-10-29 14:04:33.09
  end         2019-10-29 14:05:07.63
  </code></pre>
  
  <p>With an other example : </p>
  
  <pre><code>Date              Unavailability
  2019-10-21                   10h
  
  Flag                   Timestamp
  begin     2019-10-21 15:30:22.48
  end       2019-10-22 08:30:31.71
  </code></pre>
  
  <p>Same reflection, the alarm starts on 2019-10-21 and ends on 2019-10-22 so the unavailability of 10am is not good. Because you have to take into account the business hours. we make the difference of the dates: 22-21 = 1 so we must add:</p>
  
  <pre><code>Flag                   Timestamp
  end          2019-10-21 17:00:00
  begin        2019-10-22 07:00:00
  </code></pre>
  
  <p>with concatenate, this gives:</p>
  
  <pre><code>Date              Unavailability
  2019-10-21                  1h30
  2019-10-21                  1h30
  
  Flag                   Timestamp
  begin     2019-10-21 15:30:22.48
  end       2019-10-21 17:00:00.00
  begin     2019-10-22 07:00:00.00
  end       2019-10-22 08:30:31.71
  </code></pre>
  
  <p>A sample of my dataframe:</p>
  
  <pre><code>Flag                     Timestamp
  begin   2019-10-25 09:39:39.914889
  end     2019-10-25 09:41:09.103102
  begin   2019-10-25 10:39:58.352073
  end     2019-10-25 10:40:06.266782
  begin   2019-10-25 16:35:22.485574
  end     2019-10-27 09:50:31.713192
  begin   2019-10-29 14:04:33.095633
  end     2019-10-29 14:05:07.639344
  begin   2019-10-29 14:13:07.924966
  end     2019-10-29 14:13:08.888890
  </code></pre>
  
  <p>Thank you for your time !</p>
  
  <p>The proof :</p>
  
  <pre><code>start   Tranc   dayofMonth  lapse   TrancRecalibration
  0   2019-10-25 09:39:39.914889  begin   25.0    0.0     1
  1   2019-10-25 09:41:09.103102  end     25.0    0.0     2
  2   2019-10-25 10:39:58.352073  begin   25.0    0.0     1
  3   2019-10-25 10:40:06.266782  end     25.0    0.0     2
  4   2019-10-25 16:35:22.485574  begin   25.0    0.0     1
  5   2019-10-25 17:00:22.485574  end     NaN     0.0     2
  7   2019-10-26 07:00:39.914889  begin   NaN     1.0     1
  6   2019-10-26 17:00:39.914889  end     NaN     1.0     2
  11  2019-10-27 07:00:39.914889  begin   NaN     1.0     1
  8   2019-10-27 08:00:31.713192  begin   NaN     0.0     1
  9   2019-10-27 09:50:31.713192  end     27.0    0.0     2
  10  2019-10-27 17:00:39.914889  end     NaN     1.0     2
  15  2019-10-28 07:00:39.914889  begin   NaN     1.0     1
  12  2019-10-28 14:04:33.095633  begin   28.0    0.0     1
  14  2019-10-28 14:05:07.639344  end     28.0    0.0     2
  13  2019-10-28 14:13:07.924966  begin   28.0    0.0     1
  16  2019-10-28 14:13:08.888890  end     28.0    0.0     2
  17  2019-10-28 17:00:39.914889  end     NaN     1.0     
  </code></pre>
  
  <p>Really it's great what you did, I never succeeded ...</p>
  
  <p>Just one last point if you allow it:</p>
  
  <p>this is the result with the change from 4:55 pm to 7:00 am.</p>
  
  <p>As we can notice we have:</p>
  
  <p>begin 7:00 am
  begin 8:00 am
  end   9:50 a.m.
  end   5:00 p.m.
  begin 7:00 am
  begin 2:04 p.m.
  .
  .
  end 2:13 p.m.
  end 5:00 p.m.</p>
  
  <p>For calculation in relation to my alarms. I have to have an alternate begin end each time.
  so if I have two begin that follow I would like to remove 7:00 am and if I have two end that follow I would like to delete 5:00 pm please.</p>