code: def last_in_date_group(df,
                         data_query_cutoff_times,
                         assets,
                         reindex=True,
                         have_sids=True,
                         extra_groupers=None):
      idx = [data_query_cutoff_times[data_query_cutoff_times.searchsorted(
          df[TS_FIELD_NAME].values,
      )]]
      if have_sids:
          idx += [SID_FIELD_NAME]
      if extra_groupers is None:
          extra_groupers = []
      idx += extra_groupers
      last_in_group = df.drop(TS_FIELD_NAME, axis=1).groupby(
          idx,
          sort=False,
      ).last()
      for _ in range(len(idx) - 1):
          last_in_group = last_in_group.unstack(-1)
      if reindex:
          if have_sids:
              cols = last_in_group.columns
              last_in_group = last_in_group.reindex(
                  index=data_query_cutoff_times,
                  columns=pd.MultiIndex.from_product(
                      tuple(cols.levels[0:len(extra_groupers) + 1]) + (assets,),
                      names=cols.names,
                  ),
              )
          else:
              last_in_group = last_in_group.reindex(data_query_cutoff_times)
      return last_in_group