title: Very challenging brain tease Pandas problem: Timeseries cut and overlap
body: <p>Our data scientist is attempting to generate some fictitious data from the current <a href="https://en.wikipedia.org/wiki/Load_profile" rel="nofollow noreferrer">load profile</a> data, where the 'y' column represents some activity such as &quot;use some electricity,&quot; but we only have data for a specific time period, so we want to shift the activity to another time period. As a result, the load was expected to be moved(and added to) from period A to period B, as well as the label 'y'.</p>
  <p>So I'm looking for a function that will allow us to transfer the label and power consumption from period A to period B, and leave period A zero or low consumption in order to simulate &quot;use some electricity&quot; in period B, not in period A. When the 'y' label is True, we'd like to map 18:30 to 23:30 and 23:30 to 4:30 at next day.</p>
  <pre><code>index   ts  f1  f2  y
  8   2021-10-01 17:00:00 1044.0  0.0 False
  9   2021-10-01 17:30:00 26.0    12.0    False
  10  2021-10-01 18:00:00 16.0    10.0    False
  ---------------------------------------- P1
  11  2021-10-01 18:30:00 10.0    12.0    False
  12  2021-10-01 19:00:00 226.0   32.0    False
  13  2021-10-01 19:30:00 86.0    34.0    False
  14  2021-10-01 20:00:00 106.0   12.0    False
  15  2021-10-01 20:30:00 8.0 18.0    False
  16  2021-10-01 21:00:00 70.0    14.0    False
  17  2021-10-01 21:30:00 1604.0  0.0 False
  18  2021-10-01 22:00:00 1454.0  0.0 False
  19  2021-10-01 22:30:00 1724.0  0.0 False
  20  2021-10-01 23:00:00 1104.0  0.0 False
  ---------------------------------------- P2
  21  2021-10-01 23:30:00 1232.0  0.0 False
  22  2021-10-02 00:00:00 798.0   8.0 False
  23  2021-10-02 00:30:00 4342.0  0.0 True
  24  2021-10-02 01:00:00 4352.0  0.0 True
  25  2021-10-02 01:30:00 4258.0  0.0 True
  26  2021-10-02 02:00:00 3350.0  0.0 False
  27  2021-10-02 02:30:00 1088.0  0.0 False
  28  2021-10-02 03:00:00 1090.0  0.0 False
  29  2021-10-02 03:30:00 1558.0  0.0 False
  30  2021-10-02 04:00:00 262.0   0.0 False
  ---------------------------------------- P3
  31  2021-10-02 04:30:00 730.0   0.0 False
  32  2021-10-02 05:00:00 296.0   0.0 False
  33  2021-10-02 05:30:00 192.0   0.0 False
  34  2021-10-02 06:00:00 228.0   0.0 False
  35  2021-10-02 06:30:00 338.0   0.0 False
  ---------------------------------------- P4
  ......
  </code></pre>
  <p>Expected result</p>
  <pre><code>index   ts  f1  f2  y
  8   2021-10-01 17:00:00 1044.0  0.0 False
  9   2021-10-01 17:30:00 26.0    12.0    False
  10  2021-10-01 18:00:00 16.0    10.0    False
  ---------------------------------------- P1
  11  2021-10-01 18:30:00 10.0    12.0    False
  12  2021-10-01 19:00:00 226.0   32.0    False
  13  2021-10-01 19:30:00 86.0+4342.0 34.0+8.0    True
  14  2021-10-01 20:00:00 106.0+4352.0    12.0    True
  15  2021-10-01 20:30:00 8.0+4258.0  18.0    True
  16  2021-10-01 21:00:00 70.0    14.0    False
  17  2021-10-01 21:30:00 1604.0  0.0 False
  18  2021-10-01 22:00:00 1454.0  0.0 False
  19  2021-10-01 22:30:00 1724.0  0.0 False
  20  2021-10-01 23:00:00 1104.0  0.0 False
  ---------------------------------------- P2
  21  2021-10-01 23:30:00 1232.0  0.0 False
  22  2021-10-02 00:00:00 798.0   8.0 False
  23  2021-10-02 00:30:00 192.0   0.0 False
  24  2021-10-02 01:00:00 192.0   0.0 False
  25  2021-10-02 01:30:00 192.0   0.0 False
  26  2021-10-02 02:00:00 3350.0  0.0 False
  27  2021-10-02 02:30:00 1088.0  0.0 False
  28  2021-10-02 03:00:00 1090.0  0.0 False
  29  2021-10-02 03:30:00 1558.0  0.0 False
  30  2021-10-02 04:00:00 262.0   0.0 False
  ---------------------------------------- P3
  31  2021-10-02 04:30:00 730.0   0.0 False
  32  2021-10-02 05:00:00 296.0   0.0 False
  33  2021-10-02 05:30:00 192.0   0.0 False
  34  2021-10-02 06:00:00 228.0   0.0 False
  35  2021-10-02 06:30:00 338.0   0.0 False
  ---------------------------------------- P4
  ......
  
  </code></pre>
  <p>Raw for you to reproduce</p>
  <pre><code>{'ts': [Timestamp('2021-10-01 17:00:00'),
    Timestamp('2021-10-01 17:30:00'),
    Timestamp('2021-10-01 18:00:00'),
    Timestamp('2021-10-01 18:30:00'),
    Timestamp('2021-10-01 19:00:00'),
    Timestamp('2021-10-01 19:30:00'),
    Timestamp('2021-10-01 20:00:00'),
    Timestamp('2021-10-01 20:30:00'),
    Timestamp('2021-10-01 21:00:00'),
    Timestamp('2021-10-01 21:30:00'),
    Timestamp('2021-10-01 22:00:00'),
    Timestamp('2021-10-01 22:30:00'),
    Timestamp('2021-10-01 23:00:00'),
    Timestamp('2021-10-01 23:30:00'),
    Timestamp('2021-10-02 00:00:00'),
    Timestamp('2021-10-02 00:30:00'),
    Timestamp('2021-10-02 01:00:00'),
    Timestamp('2021-10-02 01:30:00'),
    Timestamp('2021-10-02 02:00:00'),
    Timestamp('2021-10-02 02:30:00'),
    Timestamp('2021-10-02 03:00:00'),
    Timestamp('2021-10-02 03:30:00'),
    Timestamp('2021-10-02 04:00:00'),
    Timestamp('2021-10-02 04:30:00'),
    Timestamp('2021-10-02 05:00:00'),
    Timestamp('2021-10-02 05:30:00'),
    Timestamp('2021-10-02 06:00:00'),
    Timestamp('2021-10-02 06:30:00'),
    Timestamp('2021-10-02 07:00:00'),
    Timestamp('2021-10-02 07:30:00'),
    Timestamp('2021-10-02 08:00:00'),
    Timestamp('2021-10-02 08:30:00'),
    Timestamp('2021-10-02 09:00:00'),
    Timestamp('2021-10-02 09:30:00'),
    Timestamp('2021-10-02 10:00:00'),
    Timestamp('2021-10-02 10:30:00'),
    Timestamp('2021-10-02 11:00:00'),
    Timestamp('2021-10-02 11:30:00'),
    Timestamp('2021-10-02 12:00:00'),
    Timestamp('2021-10-02 12:30:00')],
   'f1': [1044.0,
    26.0,
    16.0,
    10.0,
    226.0,
    86.0,
    106.0,
    8.0,
    70.0,
    1604.0,
    1454.0,
    1724.0,
    1104.0,
    1232.0,
    798.0,
    4342.0,
    4352.0,
    4258.0,
    3350.0,
    1088.0,
    1090.0,
    1558.0,
    262.0,
    730.0,
    296.0,
    192.0,
    228.0,
    338.0,
    1454.0,
    46.0,
    196.0,
    28.0,
    110.0,
    206.0,
    132.0,
    992.0,
    212.0,
    2462.0,
    532.0,
    1366.0],
   'f2': [0.0,
    12.0,
    10.0,
    12.0,
    32.0,
    34.0,
    12.0,
    18.0,
    14.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    8.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    14.0,
    28.0,
    8.0,
    20.0,
    26.0,
    28.0,
    22.0,
    56.0,
    4.0,
    8.0,
    6.0],
   'y': [False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    True,
    True,
    True,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False,
    False]
   }
  </code></pre>
  <p>Other solutions connect with this one, <a href="https://stackoverflow.com/questions/73822371/is-there-a-between-function-in-pandas-that-i-can-use-to-mask-time-series">here</a> and <a href="https://stackoverflow.com/questions/73823252/pandas-i-want-make-a-new-column-based-on-a-series">here</a> is the answer to the subproblem of this one.</p>