code: def normalize_timestamp_to_query_time(
      df, time, tz, inplace=False, ts_field="timestamp"
  ):
      """Update the timestamp field of a dataframe to normalize dates around
      some data query time/timezone.
  
      Parameters
      ----------
      df : pd.DataFrame
          The dataframe to update. This needs a column named ``ts_field``.
      time : datetime.time
          The time of day to use as the cutoff point for new data. Data points
          that you learn about after this time will become available to your
          algorithm on the next trading day.
      tz : tzinfo
          The timezone to normalize your dates to before comparing against
          `time`.
      inplace : bool, optional
          Update the dataframe in place.
      ts_field : str, optional
          The name of the timestamp field in ``df``.
  
      Returns
      -------
      df : pd.DataFrame
          The dataframe with the timestamp field normalized. If ``inplace`` is
          true, then this will be the same object as ``df`` otherwise this will
          be a copy.
      """
      if not inplace:
          # don't mutate the dataframe in place
          df = df.copy()
  
      # There is a pandas bug (0.18.1) where if the timestamps in a
      # normalized DatetimeIndex are not sorted and one calls `tz_localize(None)`
      #  on tha DatetimeIndex, some of the dates will be shifted by an hour
      # (similarly to the previously mentioned bug). Therefore, we must sort
      # the df here to ensure that we get the normalize correctly.
      df.sort_values(ts_field, inplace=True)
      dtidx = pd.DatetimeIndex(df.loc[:, ts_field], tz="utc")
      dtidx_local_time = dtidx.tz_convert(tz)
      to_roll_forward = mask_between_time(
          dtidx_local_time,
          time,
          _midnight,
          include_end=False,
      )
      # For all of the times that are greater than our query time add 1
      # day and truncate to the date.
      # We normalize twice here because of a bug in pandas 0.16.1 that causes
      # tz_localize() to shift some timestamps by an hour if they are not grouped
      # together by DST/EST.
      df.loc[to_roll_forward, ts_field] = (
          (
              dtidx_local_time[to_roll_forward]
              + datetime.timedelta(days=1)
          )
          .normalize()
          .tz_localize(None)
          .tz_localize("utc")
          .normalize()
      )
  
      df.loc[~to_roll_forward, ts_field] = dtidx[
          ~to_roll_forward
      ].normalize()
      return df
  
file_path: catalyst/catalyst/pipeline/loaders/utils.py
imports: import datetime
  import numpy
  import pandas
  from catalyst.pipeline.common import TS_FIELD_NAME, SID_FIELD_NAME
  from catalyst.utils.numpy_utils import categorical_dtype
  from catalyst.utils.pandas_utils import mask_between_time
label_desc: Use image node_id 7 for calling a global function with example usage: normalize_timestamp_to_query_time(df, time, tz, inplace, ts_field) and returns: df