title: How to calculate the average to the right and left inside the group from the point of change?
body: <p>I have a data set that represents the change of a certain quantity over time for each group, if the next value of the quantity differs from the previous one, then this is called the point of change. I want to calculate the average to the left and right of each point of change within each group, for example, on the horizon of 3 days. At the same time, this horizon (3 days) should not contain other points of change. If the horizon is less than 3 days either on the left or on the right, then the calculation does not need to be made. Some dates within the group may be missing.</p>
  <p>Input:</p>
  <pre><code>{'date_id': {0: '2020-10-24',
    1: '2020-10-25',
    2: '2020-10-26',
    3: '2020-10-27',
    4: '2020-10-28',
    5: '2020-10-29',
    6: '2020-10-30',
    7: '2020-11-02',
    8: '2020-11-03',
    9: '2020-11-04',
    10: '2020-11-05',
    11: '2020-11-06',
    12: '2020-11-07',
    13: '2020-11-08',
    14: '2020-11-09',
    15: '2020-11-10',
    16: '2020-11-11',
    17: '2020-11-12'},
   'group': {0: 52700,
    1: 52700,
    2: 52700,
    3: 52700,
    4: 52700,
    5: 52700,
    6: 52700,
    7: 52700,
    8: 52700,
    9: 52700,
    10: 52700,
    11: 52700,
    12: 52700,
    13: 52700,
    14: 52700,
    15: 52700,
    16: 52700,
    17: 52700},
   'quantity': {0: 1.0,
    1: 5.0,
    2: 0.0,
    3: 12.0,
    4: 0.0,
    5: 1.0,
    6: 2.0,
    7: 0.0,
    8: 2.0,
    9: 23.0,
    10: 0.0,
    11: 0.0,
    12: 16.0,
    13: 0.0,
    14: 5.0,
    15: 4.0,
    16: 0.0,
    17: 1.0},
   'change': {0: False,
    1: True,
    2: False,
    3: True,
    4: False,
    5: False,
    6: False,
    7: True,
    8: False,
    9: False,
    10: False,
    11: True,
    12: False,
    13: False,
    14: False,
    15: True,
    16: False,
    17: False}}
  </code></pre>
  <p>Expected output:</p>
  <pre><code>nan = np.nan
  
  {'date_id': {0: '2020-10-24',
    1: '2020-10-25',
    2: '2020-10-26',
    3: '2020-10-27',
    4: '2020-10-28',
    5: '2020-10-29',
    6: '2020-10-30',
    7: '2020-11-02',
    8: '2020-11-03',
    9: '2020-11-04',
    10: '2020-11-05',
    11: '2020-11-06',
    12: '2020-11-07',
    13: '2020-11-08',
    14: '2020-11-09',
    15: '2020-11-10',
    16: '2020-11-11',
    17: '2020-11-12'},
   'group': {0: 52700,
    1: 52700,
    2: 52700,
    3: 52700,
    4: 52700,
    5: 52700,
    6: 52700,
    7: 52700,
    8: 52700,
    9: 52700,
    10: 52700,
    11: 52700,
    12: 52700,
    13: 52700,
    14: 52700,
    15: 52700,
    16: 52700,
    17: 52700},
   'quantity': {0: 1.0,
    1: 5.0,
    2: 0.0,
    3: 12.0,
    4: 0.0,
    5: 1.0,
    6: 2.0,
    7: 0.0,
    8: 2.0,
    9: 23.0,
    10: 0.0,
    11: 0.0,
    12: 16.0,
    13: 0.0,
    14: 5.0,
    15: 4.0,
    16: 0.0,
    17: 1.0},
   'change': {0: False,
    1: True,
    2: False,
    3: True,
    4: False,
    5: False,
    6: False,
    7: True,
    8: False,
    9: False,
    10: False,
    11: True,
    12: False,
    13: False,
    14: False,
    15: True,
    16: False,
    17: False},
   'avg_left': {0: nan,
    1: nan,
    2: nan,
    3: nan,
    4: nan,
    5: nan,
    6: nan,
    7: nan,
    8: nan,
    9: nan,
    10: nan,
    11: 8.33,
    12: nan,
    13: nan,
    14: nan,
    15: nan,
    16: nan,
    17: nan},
   'avg_right': {0: nan,
    1: nan,
    2: nan,
    3: nan,
    4: nan,
    5: nan,
    6: nan,
    7: nan,
    8: nan,
    9: nan,
    10: nan,
    11: 7.0,
    12: nan,
    13: nan,
    14: nan,
    15: nan,
    16: nan,
    17: nan}}
  </code></pre>