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]