code: def _create_lagged_data_by_intersecting_times(
      target_series: TimeSeries,
      output_chunk_length: int,
      past_covariates: Optional[TimeSeries],
      future_covariates: Optional[TimeSeries],
      lags: Optional[Sequence[int]],
      lags_past_covariates: Optional[Sequence[int]],
      lags_future_covariates: Optional[Sequence[int]],
      max_samples_per_ts: Optional[int],
      multi_models: bool,
      check_inputs: bool,
      is_training: bool,
  ) -> Tuple[
      np.ndarray, np.ndarray, Union[pd.RangeIndex, pd.DatetimeIndex]
  ]:
      """
      Helper function called by `_create_lagged_data` that computes `X`, `y`, and `times` by
      first finding the time points in each series that *could* be used to create features/labels,
      and then finding which of these 'available' times is shared by all specified series. The lagged
      values are then extracted by finding the index of each of these 'shared times' in each series,
      and then offsetting this index by the requested lag value (if constructing `X`) or the requested
      `output_chunk_length` (if constructing `y`). This approach is used if we *cannot* assume that the
      specified series are of the same frequency.
      """
      feature_times, min_lags, _ = _get_feature_times(
          target_series,
          past_covariates,
          future_covariates,
          lags,
          lags_past_covariates,
          lags_future_covariates,
          output_chunk_length,
          is_training=is_training,
          return_min_and_max_lags=True,
          check_inputs=check_inputs,
      )
      if check_inputs:
          series_and_lags_not_specified = [
              min_lag is None for min_lag in min_lags
          ]
          raise_if(
              all(series_and_lags_not_specified),
              "Must specify at least one series-lags pair.",
          )
      shared_times = get_shared_times(*feature_times, sort=True)
      raise_if(
          shared_times is None,
          "Specified series do not share any common times for which features can be created.",
      )
      if len(shared_times) > max_samples_per_ts:
          shared_times = shared_times[-max_samples_per_ts:]
      X = []
      shared_time_idx = None
      label_shared_time_idx = None
      for i, (series_i, lags_i, min_lag_i) in enumerate(
          zip(
              [target_series, past_covariates, future_covariates],
              [lags, lags_past_covariates, lags_future_covariates],
              min_lags,
          )
      ):
          series_and_lags_specified = min_lag_i is not None
          is_target_series = is_training and (i == 0)
          if series_and_lags_specified or is_target_series:
              time_index_i = series_i.time_index
              add_to_start = (not is_target_series) and (
                  time_index_i[0] > shared_times[0]
              )
              add_to_end = (not is_target_series) and (
                  time_index_i[-1] < shared_times[-1]
              )
              if add_to_start or add_to_end:
                  new_start = shared_times[0] if add_to_start else None
                  new_end = shared_times[-1] if add_to_end else None
                  num_prepended = (
                      (time_index_i[0] - shared_times[0])
                      // series_i.freq
                      if add_to_start
                      else 0
                  )
                  time_index_i = _extend_time_index(
                      time_index_i,
                      series_i.freq,
                      new_start=new_start,
                      new_end=new_end,
                  )
              else:
                  num_prepended = 0
              shared_time_idx = (
                  np.searchsorted(time_index_i, shared_times).reshape(
                      -1, 1
                  )
                  - num_prepended
              )
          if series_and_lags_specified:
              idx_to_get = shared_time_idx + np.array(lags_i, dtype=int)
              # Before reshaping: lagged_vals.shape = (n_observations, num_lags, n_components, n_samples)
              lagged_vals = series_i.all_values(copy=False)[
                  idx_to_get, :, :
              ]
              # After reshaping: lagged_vals.shape = (n_observations, num_lags*n_components, n_samples)
              lagged_vals = lagged_vals.reshape(
                  lagged_vals.shape[0], -1, lagged_vals.shape[-1]
              )
              X.append(lagged_vals)
          # `target_series` indices required for creating labels:
          if is_target_series:
              label_shared_time_idx = shared_time_idx
      X = np.concatenate(X, axis=1)
      if is_training:
          if multi_models:
              # All points between time `t` and `t + output_chunk_length - 1` are labels:
              idx_to_get = label_shared_time_idx + np.arange(
                  output_chunk_length
              )
          else:
              # Only point at time `t + output_chunk_length - 1` is a label:
              idx_to_get = (
                  label_shared_time_idx + output_chunk_length - 1
              )
          # Before reshaping: lagged_vals.shape = (n_observations, num_lags, n_components, n_samples)
          lagged_vals = target_series.all_values(copy=False)[
              idx_to_get, :, :
          ]
          # After reshaping: lagged_vals.shape = (n_observations, num_lags*n_components, n_samples)
          y = lagged_vals.reshape(
              lagged_vals.shape[0], -1, lagged_vals.shape[-1]
          )
      else:
          y = None
      return X, y, shared_times
  
imports: import warnings
  from functools import reduce
  from math import inf
  from typing import Dict, List, Optional, Sequence, Tuple, Union
  from itertools import chain
  import numpy
  import pandas
  from numpy.lib.stride_tricks import as_strided
  from darts.logging import get_logger, raise_if, raise_if_not, raise_log
  from darts.timeseries import TimeSeries
  from darts.utils.utils import get_single_series, series2seq
label_desc: Use image node_id 8 for calling a global function with example usage: _create_lagged_data_by_intersecting_times(target_series, output_chunk_length, past_covariates, future_covariates, lags, lags_past_covariates, lags_future_covariates, max_samples_per_ts, multi_models, check_inputs, is_training) and returns: X,  y,  shared_times