pyvallocation.utils.data_helpers module

This module provides helper functions for converting between pandas DataFrames and NumPy arrays, primarily for handling financial time series data and portfolio weights. These utilities facilitate seamless data manipulation for various asset allocation and portfolio optimization tasks.

Helpers for converting between pandas and NumPy structures.

pyvallocation.utils.data_helpers.numpy_weights_to_pandas_series(weights, asset_names)[source]

Convert a 1D numpy array of weights to a pandas Series with asset names as index.

Parameters:
  • weights – Weight vector of shape (N,).

  • asset_names – Asset name list of length N.

Returns:

pd.Series – Weights indexed by asset name.

Parameters:
  • weights (numpy.ndarray)

  • asset_names (List[str])

Return type:

pandas.Series

pyvallocation.utils.data_helpers.pandas_to_numpy_returns(dataframe, price_columns=None, date_column=None, return_calculation_method='log', fill_na_method='ffill')[source]

Convert a pandas DataFrame of prices to a numpy array of returns.

Parameters:
  • dataframe – Price data as a DataFrame.

  • price_columns – Optional list of columns to treat as prices.

  • date_column – Optional column name to set as index.

  • return_calculation_method"log" or "simple" (default "log").

  • fill_na_method – Missing data handling (default "ffill").

Returns:

np.ndarray – Return matrix with shape (T-1, N).

Parameters:
  • dataframe (pandas.DataFrame)

  • price_columns (List[str] | None)

  • date_column (str | None)

  • return_calculation_method (str)

  • fill_na_method (str)

Return type:

numpy.ndarray

Functions

pyvallocation.utils.data_helpers.pandas_to_numpy_returns(dataframe, price_columns=None, date_column=None, return_calculation_method='log', fill_na_method='ffill')[source]

Convert a pandas DataFrame of prices to a numpy array of returns.

Parameters:
  • dataframe – Price data as a DataFrame.

  • price_columns – Optional list of columns to treat as prices.

  • date_column – Optional column name to set as index.

  • return_calculation_method"log" or "simple" (default "log").

  • fill_na_method – Missing data handling (default "ffill").

Returns:

np.ndarray – Return matrix with shape (T-1, N).

Parameters:
  • dataframe (pandas.DataFrame)

  • price_columns (List[str] | None)

  • date_column (str | None)

  • return_calculation_method (str)

  • fill_na_method (str)

Return type:

numpy.ndarray

pyvallocation.utils.data_helpers.numpy_weights_to_pandas_series(weights, asset_names)[source]

Convert a 1D numpy array of weights to a pandas Series with asset names as index.

Parameters:
  • weights – Weight vector of shape (N,).

  • asset_names – Asset name list of length N.

Returns:

pd.Series – Weights indexed by asset name.

Parameters:
  • weights (numpy.ndarray)

  • asset_names (List[str])

Return type:

pandas.Series