|
| 1 | +import copy |
1 | 2 | import warnings |
2 | 3 |
|
3 | 4 | import numpy as np |
@@ -815,3 +816,36 @@ def test_rank_ties_share_bucket() -> None: |
815 | 816 | # existing ``test_rank`` expectations hold. |
816 | 817 | s = mdf.MicroSeries([1, 2, 3], weights=[4, 5, 6]) |
817 | 818 | np.testing.assert_array_equal(s.rank().values, [4, 9, 15]) |
| 819 | + |
| 820 | + |
| 821 | +def test_microseries_survives_pickling(): |
| 822 | + """Weights must survive a pickle round-trip.""" |
| 823 | + import pickle |
| 824 | + |
| 825 | + s = mdf.MicroSeries([1, 2, 3], index=[7, 8, 9], weights=[1, 2, 3]) |
| 826 | + restored = pickle.loads(pickle.dumps(s)) |
| 827 | + assert isinstance(restored, mdf.MicroSeries) |
| 828 | + assert restored.sum() == 14 |
| 829 | + assert list(restored.weights) == [1.0, 2.0, 3.0] |
| 830 | + |
| 831 | + |
| 832 | +def test_microdataframe_survives_pickling(): |
| 833 | + """Weights and the weighted aggregations must survive a round-trip.""" |
| 834 | + import pickle |
| 835 | + |
| 836 | + df = mdf.MicroDataFrame( |
| 837 | + pd.DataFrame({"x": [1, 2, 3]}, index=[7, 8, 9]), weights=[1, 2, 3] |
| 838 | + ) |
| 839 | + restored = pickle.loads(pickle.dumps(df)) |
| 840 | + assert isinstance(restored, mdf.MicroDataFrame) |
| 841 | + assert isinstance(restored.weights, pd.Series) |
| 842 | + # Would be 6 (unweighted) if the aggregation overrides were not |
| 843 | + # reinstalled after unpickling. |
| 844 | + assert restored.sum()["x"] == 14 |
| 845 | + |
| 846 | + |
| 847 | +def test_deepcopy_preserves_weights(): |
| 848 | + df = mdf.MicroDataFrame(pd.DataFrame({"x": [1, 2, 3]}), weights=[1, 2, 3]) |
| 849 | + assert copy.deepcopy(df).sum()["x"] == 14 |
| 850 | + s = mdf.MicroSeries([1, 2, 3], weights=[1, 2, 3]) |
| 851 | + assert copy.deepcopy(s).sum() == 14 |
0 commit comments