Skip to content

Commit d0db036

Browse files
MaxGhenisclaude
andcommitted
Add regression test for pandas 3.0 CoW set_weights bug
This test specifically verifies the fix for the issue where `mdf["column"].set_weights()` would fail with AttributeError in pandas 3.0 with Copy-on-Write enabled, because each column access returned a new copy instead of the wrapped MicroSeries. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent cbc86fc commit d0db036

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

microdf/tests/test_pandas3_compatibility.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ def test_microdataframe_copy_independent(self):
198198
assert np.allclose(mdf_copy.weights, [1.0, 2.0, 3.0])
199199

200200

201+
def test_column_set_weights_after_access_regression(self):
202+
"""Regression test for pandas 3.0 CoW compatibility.
203+
204+
In pandas 3.0 with Copy-on-Write, modifying column.__class__ doesn't
205+
persist because each access returns a copy. This test verifies the
206+
fix that wraps columns as MicroSeries on access in __getitem__.
207+
"""
208+
mdf = MicroDataFrame(
209+
{"income": [10000, 20000, 30000]},
210+
weights=np.array([1.0, 2.0, 3.0]),
211+
)
212+
213+
# This was the exact error that occurred:
214+
# AttributeError: 'Series' object has no attribute 'set_weights'
215+
col = mdf["income"]
216+
col.set_weights(np.array([4.0, 5.0, 6.0])) # Would fail before fix
217+
218+
# Verify the new weights took effect
219+
assert np.allclose(col.weights, [4.0, 5.0, 6.0])
220+
201221
class TestGroupByWithPandas3:
202222
"""Test groupby operations with pandas 3."""
203223

0 commit comments

Comments
 (0)