groupby().agg() silently ignores weights, producing incorrect results
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 65/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
The issue is in microdf/microdataframe.py lines 644-680, where the MicroDataFrameGroupBy class does not override .agg(). Start by reading the existing weighted method overrides (like sum). Determine how to parse aggregation specs and apply weights, or implement a clear error/warning. Run the provided test case to verify the fix.
索引モデルが issue の本文から書いたものです。
説明
Problem
MicroDataFrame's groupby operations silently ignore weights when using .agg(), producing incorrect (unweighted) results without any warning or error.
Example
import microdf as mdf
import numpy as np
df = mdf.MicroDataFrame(
{"group": ["A", "A", "B", "B"], "value": [10, 20, 30, 40]},
weights=np.array([2, 3, 1, 4])
)
# CORRECT (weighted):
df.groupby("group").value.sum()
# A: 10*2 + 20*3 = 80.0
# B: 30*1 + 40*4 = 190.0
# INCORRECT (unweighted) - no warning!
df.groupby("group").agg({'value': 'sum'})
# A: 10 + 20 = 30 ❌
# B: 30 + 40 = 70 ❌
Impact
This is a critical data correctness issue because:
- Silent failure: No error or warning - just wrong numbers
- Natural usage pattern:
.agg()is a standard pandas idiom for multi-column aggregation - Plausible results: The unweighted numbers look reasonable, making bugs hard to detect
- Real-world consequences: Users analyzing survey data (CPS, ACS, etc.) will get wildly incorrect population estimates
Root Cause
Looking at microdf/microdataframe.py:644-680, the MicroDataFrameGroupBy class:
- ✓ Overrides specific methods like
sum(),mean(), etc. to apply weights - ✗ Does NOT override
.agg()or.aggregate(), so they fall back to pandas' unweighted implementation
Proposed Solutions
Option 1: Override .agg() to apply weights (Best)
- Implement
MicroDataFrameGroupBy.agg()to properly handle weights - Parse the aggregation specifications and route to weighted methods
Option 2: Raise an error (Safer than current behavior)
def agg(self, *args, **kwargs):
raise NotImplementedError(
"MicroDataFrameGroupBy.agg() does not support weights. "
"Use df.groupby(col).column.sum() instead."
)
Option 3: Emit a loud warning
def agg(self, *args, **kwargs):
warnings.warn(
"MicroDataFrameGroupBy.agg() ignores weights! Results will be unweighted.",
UserWarning,
stacklevel=2
)
return super().agg(*args, **kwargs)
Related Issues
This extends #193, which identified similar problems with .groupby()[[cols]].sum() but didn't specifically address .agg().
Additional Test Cases Needed
def test_agg_with_weights():
"""Test that .agg() applies weights correctly or raises an error"""
df = mdf.MicroDataFrame(
{"group": ["A", "A", "B"], "value": [10, 20, 30]},
weights=np.array([2, 3, 4])
)
# These should either work correctly or raise NotImplementedError
result = df.groupby("group").agg({'value': 'sum'})
# If implemented, should equal weighted sums
# A: 10*2 + 20*3 = 80
# B: 30*4 = 120
expected = pd.DataFrame({'value': [80.0, 120.0]}, index=['A', 'B'])
expected.index.name = 'group'
# Should NOT be unweighted sums (30, 30)
assert not result.equals(pd.DataFrame({'value': [30, 30]}))
Priority
HIGH - This is a data correctness bug that produces silently wrong results in a library designed for weighted survey analysis.
- 主要言語
- Python
- スター
- 16
- フォーク
- 10
- 平均マージ
- 5日 3時間
- マージ済み PR(30日)
- 21
環境構築
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
PolicyEngine/microdf のほかの issue
-
docs/examples.md still says MicroDataFrame.cov() and .corr() are unweighted対応中かも @juaristi22 が 6 日前に担当しました。 オープン
PolicyEngine/microdf#335 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Poverty gap docstrings overclaim FGT indices, and the poverty estimators have no tests対応中かも @juaristi22 が 6 日前に担当しました。 オープン
PolicyEngine/microdf#334 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Fail closed: aggregation and construction paths that silently return unweighted results対応中かも @juaristi22 が 6 日前に担当しました。 オープンbug
PolicyEngine/microdf#333 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
PolicyEngine/microdf#314 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 45/100
PolicyEngine/microdf#223 ·
メンテナーはふだん 1 日以内に返信
PolicyEngine/microdf の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜2日 初心者へのやさしさ 70/100
-
FingerprintSplitter raises ZeroDivisionError when int(frac_train * len(dataset)) floors to zeroオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 7 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
lmstudio-ai/mlx-engine#376 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
pyiron/bagofholding#166 ·