Skip to content

Always store MicroDataFrame weights as a Series - #309

Open
vahid-ahmadi wants to merge 1 commit into
mainfrom
fix/weights-always-series
Open

Always store MicroDataFrame weights as a Series#309
vahid-ahmadi wants to merge 1 commit into
mainfrom
fix/weights-always-series

Conversation

@vahid-ahmadi

Copy link
Copy Markdown

Closes #303

Problem

Two methods assigned a bare np.ndarray to self.weights, while the rest of the class assumes it is an index-aligned pd.Series:

  • nullify_weightsself.weights = np.ones(len(self))
  • set_weight_col (deprecated) — self.weights = np.array(self[column])
d = MicroDataFrame(pd.DataFrame({"x": [1, 2, 3]}), weights=[4, 5, 6])
d.nullify_weights()
d.equals(d)   # AttributeError: 'numpy.ndarray' object has no attribute 'equals'

__getitem__ and _MicroLocIndexer.__getitem__ both call self.weights.reindex(...), so column and row selection on a nullified frame were one step from the same failure. set_weight_col additionally skipped the length validation and index alignment that set_weights performs.

Change

Both methods now delegate to set_weights, which is the single place that validates length and builds pd.Series(..., index=self.index, dtype=float).

One intentional behaviour change: nullify_weights now clears weights_col (via set_weights), which is correct — after nullifying, the weights no longer come from a column. preserve_old handling in set_weight_col is unchanged; it still runs before the delegation.

Verification

  • Two new tests; both fail on main (AttributeError on equals) and pass with the change
  • make test — 64 passed on pandas 3.0.5 and pandas 2.3.3

Note: the lint job will be red until #307 merges (unrelated ruff 0.16 issue on main).

nullify_weights and the deprecated set_weight_col assigned a bare
np.ndarray to self.weights, while the rest of the class assumes an
index-aligned Series. After either call, equals() raised
AttributeError: 'numpy.ndarray' object has no attribute 'equals',
and the .weights.reindex() calls in __getitem__ and the loc indexer
were one step from the same failure. set_weight_col also skipped the
length validation and index alignment that set_weights performs.

Both now delegate to set_weights.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MicroDataFrame.nullify_weights/set_weight_col assign an ndarray, breaking equals() and reindex()

1 participant