Repository: PSLmodels/microdf

Issue:
Subsetting a MicroDataFrame doesn't subset weights
Example:
```
d = mdf.MicroDataFrame({"x": [1, 2, 3], "y": [1, 2, 2]}, weights=[4, 5, 6])
d2 = d[d.y > 1]
d2.shape  # (2, 2)
d2.weights.shape  # (3,)
```
Among other things, this breaks commands like `d[d.y > 1].mean()` with:
>TypeError: Axis must be specified when shapes of a and weights differ.

This doesn't affect MicroSeries:
```
s = mdf.MicroSeries([1, 2, 3], weights=[4, 5, 6])
s2 = s[s > 1]
s2.shape  # (2,)
s2.weights.shape  # (2,)
```

Discussion:

Patch:
diff --git a/microdf/generic.py b/microdf/generic.py
index 4b31aba..b0906cf 100644
--- a/microdf/generic.py
+++ b/microdf/generic.py
@@ -373,6 +373,11 @@ class MicroSeries(pd.Series):
     def __pos__(self, other):
         return MicroSeries(super().__pos__(other), weights=self.weights)
 
+    def __repr__(self):
+        return pd.DataFrame(
+            dict(value=self.values, weight=self.weights.values)
+        ).__repr__()
+
 
 MicroSeries.SCALAR_FUNCTIONS = [
     fn
@@ -620,7 +625,10 @@ class MicroDataFrame(pd.DataFrame):
     def __getitem__(self, key):
         result = super().__getitem__(key)
         if isinstance(result, pd.DataFrame):
-            weights = self.weights
+            try:
+                weights = self.weights[key]
+            except Exception:
+                weights = self.weights
             return MicroDataFrame(result, weights=weights)
         return result
 
@@ -755,3 +763,8 @@ class MicroDataFrame(pd.DataFrame):
         """
         in_poverty = income < threshold
         return in_poverty.sum()
+
+    def __repr__(self):
+        df = pd.DataFrame(self)
+        df["weight"] = self.weights
+        return df[[df.columns[-1]] + list(df.columns[:-1])].__repr__()


Test patch:
diff --git a/microdf/tests/test_generic.py b/microdf/tests/test_generic.py
index 3dfaffa..3a1a58e 100644
--- a/microdf/tests/test_generic.py
+++ b/microdf/tests/test_generic.py
@@ -200,3 +200,9 @@ def test_subset():
     df_no_z_diff_weights = df_no_z.copy()
     df_no_z_diff_weights.weights += 1
     assert not df[["x", "y"]].equals(df_no_z_diff_weights)
+
+
+def test_value_subset():
+    d = mdf.MicroDataFrame({"x": [1, 2, 3], "y": [1, 2, 2]}, weights=[4, 5, 6])
+    d2 = d[d.y > 1]
+    assert d2.y.shape == d2.weights.shape