Input Handlers#

Concrete input handlers for loading portfolio and benchmark weights (and the benchmark returns file, which has the same layout) from CSV and Excel files, plus the contract they implement. See Data Formats for the expected file layouts.

CSV portfolio input handler for reading portfolio allocation files.

Read portfolio weight files in CSV format, supporting both horizontal (Ticker | Date1 | Date2 | …) and vertical (Date | Ticker1 | Ticker2 | …) layouts with automatic format detection and transposition.

class CsvPortfolioInputHandler(base_dir: str)[source]#

Bases: PortfolioInputHandlerInterface

Handles reading and parsing of CSV portfolio files.

This class reads a portfolio CSV file, infers decimal precision dynamically for numeric columns, and converts them into a typed PyArrow table suitable for financial pipelines.

Supports both horizontal and vertical formats:
  • Horizontal: Ticker | Date1 | Date2 | …

  • Vertical: Date | Ticker1 | Ticker2 | … (auto-transposes to horizontal)

Variables:

base_dir (Path) – Directory where portfolio CSV files are located.

read_portfolio(*, file_name: str) → Table[source]#

Reads a portfolio from a CSV file and converts it into a typed PyArrow Table.

Automatically detects horizontal vs vertical format and returns horizontal format.

Parameters:

file_name (str) – Name of the CSV file (with or without .csv extension).

Returns:

Processed PyArrow table containing portfolio tickers and weights in horizontal format.

Return type:

pa.Table

Raises:
  • FileNotFoundError – If the file does not exist in the specified base directory.

  • ReadingCsvPortfolioError – If the CSV cannot be read or processed correctly.

Excel portfolio input handler for reading .xlsx portfolio allocation files.

Read portfolio weight files in Excel format, supporting both horizontal (Ticker | Date1 | Date2 | …) and vertical (Date | Ticker1 | Ticker2 | …) layouts with automatic format detection, transposition, and decimal precision inference.

class ExcelPortfolioInputHandler(base_dir: str)[source]#

Bases: PortfolioInputHandlerInterface

Professional Excel portfolio file handler for financial systems.

This class provides a robust interface for reading and processing Excel-based portfolio files (.xlsx format). It automatically handles data type inference, decimal precision detection for financial data, and converts portfolio information into optimized PyArrow tables for high-performance analytics.

Supports both horizontal and vertical formats:
  • Horizontal: Ticker | Date1 | Date2 | …

  • Vertical: Date | Ticker1 | Ticker2 | … (auto-transposes to horizontal)

Variables:

base_dir (pathlib.Path) – The base directory path where Excel portfolio files are stored.

read_portfolio(*, file_name: str) → Table[source]#

Read and process an Excel portfolio file into a typed PyArrow table.

This method performs comprehensive processing of Excel portfolio files, including automatic file extension handling, data type inference, decimal precision detection, and conversion to an optimized PyArrow table format.

Automatically detects horizontal vs vertical format and returns horizontal format.

Parameters:

file_name (str) – The name of the Excel portfolio file to read. Can be provided with or without the .xlsx extension.

Returns:

A typed PyArrow table containing the processed portfolio data in horizontal format.

Return type:

pyarrow.Table

Raises:

Column-selective CSV reading, used for the per-factor return files.

read_csv_intersection reads only the requested columns that exist in a file.

read_csv_intersection(path, wanted_cols, index_col=0, **kwargs)[source]#

Abstract interface for portfolio file input handlers.

Define the contract that all portfolio input handlers (CSV, Excel, etc.) must implement to provide consistent portfolio data loading across different file formats.

class PortfolioInputHandlerInterface[source]#

Bases: object

Abstract base class for portfolio input handlers.

Concrete implementations must provide a read_portfolio method that reads a portfolio file and returns a PyArrow Table with ticker identifiers and allocation weights indexed by rebalance dates.

abstractmethod read_portfolio(*, file_name: str) → Table[source]#

Reads a portfolio file and returns a DataFrame with: - Index: tickers - Columns: rebalance dates as datetime - Values: weights (floats between 0 and 1)

Parameters:

file_name (str) – File name of the portfolio to read

Return type:

pa.Table