Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/quant_timing/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _available(index: pd.Index, available: pd.DataFrame | None) -> pd.DataFrame:
if available is None:
return pd.DataFrame(True, index=index, columns=columns)
aligned = available.reindex(index=index, columns=columns)
return aligned.fillna(False).astype(bool)
return aligned.astype("boolean").fillna(False).astype(bool)


def _contract_exposure(
Expand Down
9 changes: 6 additions & 3 deletions src/quant_timing/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,21 @@ def _group_weights(
raise ValueError(f"style group {group['name']} needs an amount panel")
amount_window = int(group.get("amount_window", 60))
trailing = amounts.reindex(index=prices.index, columns=members).rolling(amount_window).sum()
benchmark = trailing.div(trailing.sum(axis=1).replace(0, pd.NA), axis=0) * group_weight
activity = trailing.sum(axis=1)
benchmark = trailing.div(activity.where(activity.ne(0)), axis=0) * group_weight
if cap is not None:
raw = _cap_to_benchmark(raw, benchmark, float(cap), group_weight)
else:
raw = raw.clip(lower=0.0)
total = raw.sum(axis=1).replace(0, pd.NA)
total = raw.sum(axis=1)
total = total.where(total.ne(0))
raw = raw.div(total, axis=0) * group_weight
return ready, {member: raw[member] for member in members}
if cap is not None:
raw = raw.clip(lower=base - float(cap), upper=base + float(cap))
raw = raw.clip(lower=0.0)
total = raw.sum(axis=1).replace(0, pd.NA)
total = raw.sum(axis=1)
total = total.where(total.ne(0))
raw = raw.div(total, axis=0) * group_weight
return ready, {member: raw[member] for member in members}

Expand Down
48 changes: 48 additions & 0 deletions tests/test_pandas_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import warnings

import numpy as np
import pandas as pd

from quant_timing.overlay import _available
from quant_timing.style import style_internal_weights


def test_amount_benchmark_keeps_numeric_dtype_and_active_weight_limits():
dates = pd.bdate_range("2026-01-02", periods=8)
prices = pd.DataFrame({"A": range(10, 18), "B": range(20, 28)}, index=dates)
amounts = pd.DataFrame(
{"A": [0.0, 0.0] + [100.0] * 6, "B": [0.0, 0.0] + [300.0] * 6}, index=dates
)
style = {
"return_window": 1,
"tilt": 0.7,
"threshold": 0.0,
"groups": [
{
"name": "size",
"members": ["A", "B"],
"group_weight": 1.0,
"benchmark": "amount_share",
"amount_window": 2,
"max_active_deviation": 0.1,
}
],
}
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
weights = style_internal_weights(prices, style, amounts=amounts)
assert all(pd.api.types.is_float_dtype(dtype) for dtype in weights.dtypes)
assert weights.iloc[0].isna().all()
np.testing.assert_allclose(weights.iloc[1:].sum(axis=1), 1.0)
assert weights.loc[dates[3] :, "A"].between(0.15 - 1e-10, 0.35 + 1e-10).all()
assert weights.loc[dates[3] :, "B"].between(0.65 - 1e-10, 0.85 + 1e-10).all()


def test_missing_contract_availability_stays_false_without_implicit_downcast():
dates = pd.bdate_range("2026-01-02", periods=3)
available = pd.DataFrame({"IF": [True, False]}, index=dates[:2])
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
actual = _available(dates, available)
expected = pd.DataFrame({"IF": [True, False, False], "IC": False, "IM": False}, index=dates)
pd.testing.assert_frame_equal(actual, expected)
Loading