From fa9e5db26165f60f5559cbae14e755fbf33b385e Mon Sep 17 00:00:00 2001 From: PureSaber <88314620+PureSaber@users.noreply.github.com> Date: Sat, 3 Oct 2026 09:46:35 +0800 Subject: [PATCH] fix: preserve numeric and boolean dtypes across pandas versions --- src/quant_timing/overlay.py | 2 +- src/quant_timing/style.py | 9 ++++-- tests/test_pandas_compatibility.py | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/test_pandas_compatibility.py diff --git a/src/quant_timing/overlay.py b/src/quant_timing/overlay.py index 0e1dfa7..5edc5e8 100644 --- a/src/quant_timing/overlay.py +++ b/src/quant_timing/overlay.py @@ -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( diff --git a/src/quant_timing/style.py b/src/quant_timing/style.py index 933b071..fc2ba0c 100644 --- a/src/quant_timing/style.py +++ b/src/quant_timing/style.py @@ -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} diff --git a/tests/test_pandas_compatibility.py b/tests/test_pandas_compatibility.py new file mode 100644 index 0000000..52b0b2b --- /dev/null +++ b/tests/test_pandas_compatibility.py @@ -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)