skfolio.containers.concat#

skfolio.containers.concat(items, *, verify_observations=False)[source]#

Concatenate containers along the observation axis.

Panels and panel views must have identical assets and field names in the same order, field types, field dtypes, inactive_policy values, categorical levels and 3D field metadata. Field arrays, observations, active_mask and estimation_mask are concatenated on axis 0.

Graph series concatenate observation labels and graph references. Graphs may have different nodes, storage formats and dtypes. Repeated references share the original graph objects.

Parameters:
itemsiterable of AssetPanel, AssetPanelView or GraphSeries

Containers to concatenate, in the order they should appear in the result. The iterable must contain at least one object. Panels and panel views can be combined, but cannot be mixed with graph series.

verify_observationsbool, default=False

If True, raise an error when the concatenated observation labels contain duplicates. AssetPanel requires unique observations even when False; GraphSeries preserves repeated labels when False.

Returns:
containerAssetPanel or GraphSeries

Concatenated container. Panels and panel views produce an AssetPanel; graph series produce a GraphSeries.

Raises:
TypeError

If the inputs mix panels and graph series, contain unsupported objects, or have mismatched field types or dtypes.

ValueError

If the iterable is empty, panels have mismatched assets or field metadata, or duplicate observation labels are not allowed.

Examples

Concatenate two panels:

>>> from skfolio.containers import AssetPanel, concat
>>> panel_1 = AssetPanel(
...     fields={"returns": [[0.01, 0.02]]},
...     observations=["2024-01-01"],
...     asset_names=["A", "B"],
... )
>>> panel_2 = AssetPanel(
...     fields={"returns": [[0.03, 0.04]]},
...     observations=["2024-01-02"],
...     asset_names=["A", "B"],
... )
>>> concat([panel_1, panel_2])
AssetPanel(n_observations=2, n_assets=2, n_fields=1)

Concatenate graph series while preserving shared graphs:

>>> from skfolio.containers import Graph, GraphSeries
>>> graph = Graph([[1.0]], nodes=["A"])
>>> first = GraphSeries([graph], observations=[0])
>>> second = GraphSeries([graph, None], observations=[1, 2])
>>> combined = concat([first, second])
>>> combined
GraphSeries(n_observations=3, n_graphs=1)
>>> combined[0] is combined[1] is graph
True