From 7ed31878453382ef3c97ff4a1f2c4fdba73bd28d Mon Sep 17 00:00:00 2001 From: vahid-ahmadi Date: Mon, 31 Aug 2026 17:36:01 +0100 Subject: [PATCH] Align nullify_weights to the Series index MicroSeries.nullify_weights built its all-ones weight Series without an index, so it carried a default RangeIndex. Weighted operations are label-aligned (self.multiply(self.weights) in sum()/weight()), so on any non-default index the multiply produced all-NaN and every aggregation collapsed to 0: s = MicroSeries([1, 2, 3], index=[10, 11, 12], weights=[1, 2, 3]) s.nullify_weights() s.sum() # 0.0, expected 6.0 Same bug class as #283, which fixed set_weights but not this path. Co-Authored-By: Claude Opus 5 (1M context) --- changelog.d/nullify-weights-index.yaml | 5 +++++ microdf/microseries.py | 6 +++++- microdf/tests/test_microseries_dataframe.py | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 changelog.d/nullify-weights-index.yaml diff --git a/changelog.d/nullify-weights-index.yaml b/changelog.d/nullify-weights-index.yaml new file mode 100644 index 0000000..4f32674 --- /dev/null +++ b/changelog.d/nullify-weights-index.yaml @@ -0,0 +1,5 @@ +- bump: patch + changes: + fixed: + - MicroSeries.nullify_weights now aligns weights to the Series index, so + aggregations no longer return 0 on a non-default index. diff --git a/microdf/microseries.py b/microdf/microseries.py index 6ca3a33..d572dde 100644 --- a/microdf/microseries.py +++ b/microdf/microseries.py @@ -157,7 +157,11 @@ def nullify_weights(self) -> None: This is useful for comparing weighted and unweighted statistics or when you want to temporarily ignore weights. """ - self.weights = pd.Series(np.ones(len(self)), dtype=float) + # Index the ones against self.index: weighted ops are label-aligned + # (self.multiply(self.weights) in .sum()/.weight()), so a default + # RangeIndex here silently produces all-NaN and collapses every + # aggregation to 0 whenever the caller uses a non-default index. + self.weights = pd.Series(np.ones(len(self)), index=self.index, dtype=float) @vector_function def weight(self) -> pd.Series: diff --git a/microdf/tests/test_microseries_dataframe.py b/microdf/tests/test_microseries_dataframe.py index a73e8bb..cd0ae14 100644 --- a/microdf/tests/test_microseries_dataframe.py +++ b/microdf/tests/test_microseries_dataframe.py @@ -815,3 +815,12 @@ def test_rank_ties_share_bucket() -> None: # existing ``test_rank`` expectations hold. s = mdf.MicroSeries([1, 2, 3], weights=[4, 5, 6]) np.testing.assert_array_equal(s.rank().values, [4, 9, 15]) + + +def test_nullify_weights_non_default_index(): + """nullify_weights must align to the index, not a fresh RangeIndex.""" + s = mdf.MicroSeries([1, 2, 3], index=[10, 11, 12], weights=[1, 2, 3]) + s.nullify_weights() + assert s.sum() == 6 + assert s.mean() == 2 + assert list(s.weights.index) == [10, 11, 12]