Source code for kaxanuk.attribution_analysis.modules.table_conversion

"""
Conversion utilities for bridging PyArrow tables to pandas DataFrames.

Provides functions to convert PyArrow tables produced by the market data
and portfolio pipelines into pandas DataFrames with proper datetime indexing,
as well as a standalone returns computation function.
"""

import pandas as pd
import pyarrow as pa


[docs] def pyarrow_table_to_pandas( table: pa.Table, date_column: str, ) -> pd.DataFrame: df = table.to_pandas() df[date_column] = pd.to_datetime(df[date_column]) df = df.set_index(date_column) df.index.name = None # Convert decimal.Decimal columns (from PyArrow decimal256) to float64 # so downstream pandas arithmetic (*, +, etc.) works with native floats numeric_cols = df.columns df[numeric_cols] = df[numeric_cols].astype(float) return df
[docs] def portfolio_table_to_pandas(table: pa.Table) -> pd.DataFrame: df = table.to_pandas() df = df.set_index("Ticker").T try: df.index = pd.to_datetime(df.index, format='%Y-%m-%d') except (ValueError, TypeError): df.index = pd.to_datetime(df.index, format="%d/%m/%Y") df.index.name = None return df.astype(float)
[docs] def compute_returns_from_prices(price_df: pd.DataFrame) -> pd.DataFrame: returns = price_df.pct_change(fill_method=None) return returns.dropna(axis='columns', how='all')