code: def fix_location(records, max_elapsed_seconds=300):
      
      groups = itertools.groupby(records, lambda r: r.direction)
      groups = [(interaction, list(g)) for interaction, g in groups]
  
      def tdist(t1, t2):
              return abs((t1 - t2).total_seconds())
  
      for i, (interaction, g) in enumerate(groups):
          if interaction == 'in':
              continue
  
          prev_gps = groups[i-1][1][-1]
          next_gps = groups[i+1][1][0]
  
          for r in g:
              if tdist(r.datetime, prev_gps.datetime) <= max_elapsed_seconds:
                  r.position = prev_gps.position
              elif tdist(r.datetime, next_gps.datetime) <= max_elapsed_seconds:
                  r.position = next_gps.position
docs: Update position of all records based on the position of
      the closest GPS record.
  
      .. note:: Use this function when call and text records are missing a
                location, but you have access to accurate GPS traces.