code: def expand_events(  # type:ignore
      events: Union[
          List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]],
          Dict[
              str,
              List[
                  Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]
              ],
          ],
          pd.Series,
          pd.DataFrame,
      ],
      left_expand: Union[pd.Timedelta, str, int] = 0,
      right_expand: Union[pd.Timedelta, str, int] = 0,
      freq_as_period: bool = True,
  ) -> Union[
      List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]],
      Dict[
          str,
          List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]],
      ],
      pd.Series,
      pd.DataFrame,
  ]:
      """Expand duration of events.
  
      Parameters
      ----------
      events: list, dict, pandas Series, or pandas DataFrame
          Events to be expanded.
  
          - If list, a list of events where an event is a pandas Timestamp if it
            is instantaneous or a 2-tuple of pandas Timestamps if it is a closed
            time interval.
          - If dict, each key-value pair represents an independent list of
            events.
          - If pandas Series, it is binary where 1 represents events cover this
            time point.
          - If pandas DataFrame, each column is treated as an independent Series.
  
      left_expand: pandas Timedelta, str, or int, optional
          Time range to expand backward.
  
          - If str, it must be able to be converted into a pandas Timedelta
            object.
          - If int, it must be in nanosecond.
  
          Default: 0.
  
      right_expand: pandas Timedelta, str, or int, optional
          Time range to expand forward.
  
          - If str, it must be able to be converted into a pandas Timedelta
            object.
          - If int, it must be in nanosecond.
  
          Default: 0.
  
      freq_as_period: bool, optional
          Whether to regard time index with regular frequency (i.e. attribute
          `freq` of time index is not None) as time intervals. Only used when
          input events is pandas Series or DataFrame.
  
          For example, DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03',
          '2017-01-04', '2017-01-05'], dtype='datetime64[ns]', freq='D') has
          daily frequency. If freq_as_period=True, each time point in the index
          represents that day (24 hours). Otherwsie, each time point represents
          the instantaneous time instance of 00:00:00 on that day.
  
          Default: True.
  
      Returns
      -------
      list, dict, pandas Series, or pandas DataFrame
          Expanded events.
  
      """
  
      if not isinstance(left_expand, pd.Timedelta):
          left_expand = pd.Timedelta(left_expand)
      if not isinstance(right_expand, pd.Timedelta):
          right_expand = pd.Timedelta(right_expand)
  
      if isinstance(events, pd.Series):
          labels = validate_series(events)  # type: pd.Series
          lists = to_events(
              labels, freq_as_period=freq_as_period
          )  # type:List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]]
          expanded_lists = expand_events(  # type:ignore
              events=lists,
              left_expand=left_expand,
              right_expand=right_expand,
          )  # type:List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]]
          expanded_labels = to_labels(
              lists=expanded_lists,
              time_index=labels.index,
              freq_as_period=freq_as_period,
          )  # type: pd.Series
          expanded_labels.loc[
              (expanded_labels == False) & (labels.isna())
          ] = float("nan")
          expanded_labels = expanded_labels.rename(labels.name)
          expanded_labels.index = labels.index
          return expanded_labels
      elif isinstance(events, pd.DataFrame):
          expanded_df = pd.concat(
              [
                  expand_events(
                      s,
                      left_expand=left_expand,
                      right_expand=right_expand,
                      freq_as_period=freq_as_period,
                  )
                  for _, s in events.iteritems()
              ],
              axis=1,
          )  # type: pd.DataFrame
          return expanded_df
      elif isinstance(events, list):
          expanded_list = (
              []
          )  # type: List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]]
          for ano in events:
              if isinstance(ano, tuple):
                  expanded_list.append(
                      (ano[0] - left_expand, ano[1] + right_expand)
                  )
              else:
                  expanded_list.append(
                      (ano - left_expand, ano + right_expand)
                  )
          expanded_list = validate_events(expanded_list)
          return expanded_list
      elif isinstance(events, dict):
          return {
              key: expand_events(value, left_expand, right_expand)
              for key, value in events.items()
          }
      else:
          raise TypeError(
              "Arugment `events` must be a list or a dict of lists."
          )
  
file_path: adtk/src/adtk/data/_data.py
imports: from typing import Dict, List, Optional, Tuple, Union, overload
  import numpy
  import pandas
label_desc: Use image node_id 13 for calling a global function with example usage: expand_events(events, left_expand, right_expand, freq_as_period) and returns: expanded_labels, expanded_df, expanded_list, unknown