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
6 changes: 6 additions & 0 deletions changelog.d/weights-always-series.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- bump: patch
changes:
fixed:
- MicroDataFrame.nullify_weights and set_weight_col now store weights as an
index-aligned Series instead of a bare ndarray, so equals() no longer
raises.
12 changes: 7 additions & 5 deletions microdf/microdataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,20 @@ def set_weight_col(self, column: str, preserve_old: Optional[bool] = False) -> N
if preserve_old and self.weights_col is not None:
self["old_" + self.weights_col] = self.weights

self.weights = np.array(self[column])
self.weights_col = column
self._link_all_weights()
# Delegate to set_weights: it validates length and builds an
# index-aligned float Series rather than a bare ndarray.
self.set_weights(column)

def nullify_weights(self) -> None:
"""Set all weights to 1, effectively making the DataFrame unweighted.

This is useful for comparing weighted and unweighted statistics or when
you want to temporarily ignore weights.
"""
self.weights = np.ones(len(self))
self._link_all_weights()
# Route through set_weights so self.weights stays an index-aligned
# float Series. Assigning a bare ndarray here broke every caller
# that treats it as a Series (equals(), reindex() in __getitem__).
self.set_weights(np.ones(len(self)))

def __getitem__(
self, key: Union[str, List]
Expand Down
22 changes: 22 additions & 0 deletions microdf/tests/test_microseries_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,25 @@ 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_weights_stay_a_series_after_nullify():
"""nullify_weights must leave weights as an index-aligned Series."""
df = mdf.MicroDataFrame(pd.DataFrame({"x": [1, 2, 3]}), weights=[4, 5, 6])
df.nullify_weights()
assert isinstance(df.weights, pd.Series)
assert list(df.weights.index) == list(df.index)
assert df.equals(df)
assert df.sum()["x"] == 6


def test_weights_stay_a_series_after_set_weight_col():
"""The deprecated set_weight_col must also produce a Series."""
df = mdf.MicroDataFrame(pd.DataFrame({"x": [1, 2, 3], "w": [1.0, 2.0, 3.0]}))
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
df.set_weight_col("w")
assert isinstance(df.weights, pd.Series)
assert df.weights_col == "w"
assert df.equals(df)
assert df.sum()["x"] == 14
Loading