groupby().agg() silently ignores weights, producing incorrect results
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 65/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Bereich
- backend-api-design, data, data-engineering
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Python
- Sterne
- 16
- Forks
- 10
- Ø Merge
- 5 T. 3 Std.
- Gemergte PRs (30 T.)
- 21
Entwicklungsumgebung
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus PolicyEngine/microdf
-
docs/examples.md still says MicroDataFrame.cov() and .corr() are unweightedEvtl. vergeben @juaristi22 hat das vor 6 Tagen übernommen. Offen
PolicyEngine/microdf#335 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Poverty gap docstrings overclaim FGT indices, and the poverty estimators have no testsEvtl. vergeben @juaristi22 hat das vor 6 Tagen übernommen. Offen
PolicyEngine/microdf#334 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Fail closed: aggregation and construction paths that silently return unweighted resultsEvtl. vergeben @juaristi22 hat das vor 6 Tagen übernommen. Offenbug
PolicyEngine/microdf#333 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 30/100
PolicyEngine/microdf#314 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 45/100
PolicyEngine/microdf#223 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in PolicyEngine/microdf
Ähnliche Issues
-
ACK_WAITING HELP_WANTED UPDATE_CS
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
OWASP/CheatSheetSeries#2458 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 90/100
BasedHardware/omi#19711 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Qwen3_5MoeModel no longer returns router_logits, breaking aux loss with output_router_logits=TrueOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
huggingface/transformers#49172 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
vllm-project/vllm-metal#885 ·
Maintainer antworten meist innerhalb von 1 Tag