Skip to content
Open
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
5 changes: 5 additions & 0 deletions changelog.d/nullify-weights-index.yaml
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 5 additions & 1 deletion microdf/microseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions microdf/tests/test_microseries_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading