Portfolio Transformer#
Format detection and table transposition utilities.
Portfolio data transformation utilities.
This module provides functions for transforming portfolio data between different representations and formats.
Functions are organized by transformation type: - Format Detection: detect_portfolio_format - Table Operations: transpose_vertical_to_horizontal - Date Validation: validate_portfolio_date_range - Entity Conversion: entity_to_weights_dict
- detect_portfolio_format(first_column_name: str, error_class: type[Exception]) str[source]#
Detect portfolio layout (horizontal vs vertical) based on the first column name.
Horizontal format starts with “Ticker”; vertical format starts with one of “date_column”, “date” or “m_date”. Names are compared case-insensitively after stripping surrounding whitespace.
- Parameters:
first_column_name (str) – Name of the first column in the portfolio file.
error_class (Type[Exception]) – Exception class to raise if format is invalid.
- Returns:
“horizontal” or “vertical”.
- Return type:
str
- Raises:
error_class – If the first column is neither an accepted ticker nor date name.
- entity_to_weights_dict(entity: PortfolioEntity) dict[Timestamp, dict[str, Scalar]][source]#
Convert a PortfolioEntity into a time-indexed weights dictionary.
The result maps normalized timestamps (rebalancing dates) to “ticker -> weight” maps. Dates are parsed using supported formats and normalized to midnight.
- Parameters:
entity (PortfolioEntity) – A validated portfolio entity containing asset weights organized by date columns.
- Returns:
Mapping from rebalancing date to ticker weights.
- Return type:
dict[pd.Timestamp, dict[str, pa.Scalar]]
- Raises:
PortfolioEntityError – If date columns cannot be parsed using supported formats.
Notes
Only date columns with valid weights are kept.
Empty weight sets for specific dates are excluded.
- transpose_vertical_to_horizontal(table: Table) Table[source]#
Transpose a vertical-format table (dates as rows) into the horizontal format (tickers as rows).
Portfolios may be given with one row per date and one column per ticker; the engine needs one row per ticker and one column per date:
Vertical (input) Horizontal (output) Date | AAPL | MSFT | ... Ticker | 2024-01-01 | 2024-01-02 | ... 2024-01-01 | 0.5 | 0.3 | ... AAPL | 0.5 | 0.6 | ... 2024-01-02 | 0.6 | 0.2 | ... MSFT | 0.3 | 0.2 | ...
- Parameters:
table (pa.Table) – Vertical-format table: the first column holds the dates (strings), each other column one ticker.
- Returns:
Horizontal-format table: a
Tickercolumn, then one column per date; one row per ticker.- Return type:
pa.Table
Notes
Date values stay strings (they are not cast to timestamps), the order of tickers and dates is preserved, and the weights are neither normalized nor validated.
- validate_portfolio_date_range(table: Table, start_date: date | Literal['auto'] | None, end_date: date | Literal['auto'] | None, source_name: str) None[source]#
Fail fast if the configured date range doesn’t overlap a portfolio table’s dates.
Parses the table’s date columns (every column but “Ticker”) and raises immediately when start_date/end_date exclude all of them. Portfolio and benchmark weight files are cheap to load, so this check is meant to run right after they’re read and before any heavier market data, factor returns, or benchmark files are loaded — surfacing a misconfigured date range in milliseconds instead of after minutes of unrelated I/O.
- Parameters:
table (pa.Table) – Horizontal-format table with columns
[Ticker, date1, date2, ...].start_date (datetime.date or {"auto"} or None) – Configured lower bound.
"auto"orNoneskips the check.end_date (datetime.date or {"auto"} or None) – Configured upper bound.
"auto"orNoneskips the check.source_name (str) – Human-readable description of the table’s origin, used in the error message (e.g.
"portfolio 'portfolio_weights'").
- Raises:
DateRangeError – If start_date is after every date in table, or end_date is before every date in table.