def series2seq(
    ts: Optional[
        Union[TimeSeries, Sequence[TimeSeries], Sequence[Sequence[TimeSeries]]]
    ],
    seq_type_out: SeriesType = SeriesType.SEQ,
    nested: bool = False,
) -> Optional[Union[TimeSeries, Sequence[TimeSeries], Sequence[Sequence[TimeSeries]]]]:
    """If possible, converts `ts` into the desired sequence type `seq_type_out`. Otherwise, returns the
    original `ts`.

    Parameters
    ----------
    ts
        None, a single TimeSeries, a sequence of TimeSeries, or a sequence of sequences of TimeSeries.
    seq_type_out
        The output sequence type:

        - SeriesType.SINGLE: `TimeSeries` (e.g. a single series)
        - SeriesType.SEQ: sequence of `TimeSeries` (e.g. multiple series)
        - SeriesType.SEQ_SEQ: sequence of sequences of `TimeSeries` (e.g. historical forecasts output)
    nested
        Only applies with `seq_type_out=SeriesType.SEQ_SEQ` and `ts` having a sequence type `SeriesType.SEQ`.
        In this case, wrap each element in `ts` in a list ([ts1, ts2] -> [[ts1], [ts2]]).

    Raises
    ------
    ValueError
        If there is an invalid `seq_type_out` value.
    """
    if ts is None:
        return ts

    if not isinstance(seq_type_out, SeriesType):
        raise_log(
            ValueError(
                f"Invalid parameter `seq_type_out={seq_type_out}`. Must be one of `(0, 1, 2)`"
            ),
            logger=logger,
        )

    seq_type_in = get_series_seq_type(ts)

    if seq_type_out == seq_type_in:
        return ts

    n_series = 1 if seq_type_in == SeriesType.SINGLE else len(ts)

    if seq_type_in == SeriesType.SINGLE and seq_type_out == SeriesType.SEQ:
        # ts -> [ts]
        return [ts]
    elif seq_type_in == SeriesType.SINGLE and seq_type_out == SeriesType.SEQ_SEQ:
        # ts -> [[ts]]
        return [[ts]]
    elif (
        seq_type_in == SeriesType.SEQ
        and seq_type_out == SeriesType.SINGLE
        and n_series == 1
    ):
        # [ts] -> ts
        return ts[0]
    elif seq_type_in == SeriesType.SEQ and seq_type_out == SeriesType.SEQ_SEQ:
        if not nested:
            # [ts1, ts2] -> [[ts1, ts2]]
            return [ts]
        else:
            # [ts1, ts2] -> [[ts1], [ts2]]
            return [[ts_] for ts_ in ts]
    elif (
        seq_type_in == SeriesType.SEQ_SEQ
        and seq_type_out == SeriesType.SINGLE
        and n_series == 1
    ):
        # [[ts]] -> [ts]
        return ts[0]
    elif (
        seq_type_in == SeriesType.SEQ_SEQ
        and seq_type_out == SeriesType.SEQ
        and n_series == 1
    ):
        # [[ts1, ts2]] -> [[ts1, ts2]]
        return ts
    else:
        # ts -> ts
        return ts
If possible, converts `ts` into the desired sequence type `seq_type_out`. Otherwise, returns the
    original `ts`.

    Parameters
    ----------
    ts
        None, a single TimeSeries, a sequence of TimeSeries, or a sequence of sequences of TimeSeries.
    seq_type_out
        The output sequence type:

        - SeriesType.SINGLE: `TimeSeries` (e.g. a single series)
        - SeriesType.SEQ: sequence of `TimeSeries` (e.g. multiple series)
        - SeriesType.SEQ_SEQ: sequence of sequences of `TimeSeries` (e.g. historical forecasts output)
    nested
        Only applies with `seq_type_out=SeriesType.SEQ_SEQ` and `ts` having a sequence type `SeriesType.SEQ`.
        In this case, wrap each element in `ts` in a list ([ts1, ts2] -> [[ts1], [ts2]]).

    Raises
    ------
    ValueError
        If there is an invalid `seq_type_out` value.
    
darts/utils/ts_utils.py