Skip to content

Commit d546056

Browse files
MaxGhenisclaude
andcommitted
Fix groupby aggregation to return plain DataFrame without spurious weights
Aggregated results from groupby operations should be plain DataFrames, not MicroDataFrames, since the weights have already been applied during aggregation. This fixes the NaN weight column issue noted in review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 36a4066 commit d546056

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

microdf/microdataframe.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -765,11 +765,9 @@ def fn(*args, **kwargs):
765765
except Exception:
766766
# Skip columns that can't be aggregated
767767
pass
768-
return (
769-
MicroDataFrame(results)
770-
if results
771-
else MicroDataFrame()
772-
)
768+
# Return plain DataFrame - aggregated results don't have
769+
# per-row weights (weights were already applied)
770+
return pd.DataFrame(results) if results else pd.DataFrame()
773771

774772
return fn
775773

@@ -787,11 +785,9 @@ def fn(*args, **kwargs) -> Union[pd.Series, pd.DataFrame]:
787785
except Exception:
788786
# Skip columns that can't be aggregated
789787
pass
790-
return (
791-
MicroDataFrame(results)
792-
if results
793-
else MicroDataFrame()
794-
)
788+
# Return plain DataFrame - aggregated results don't have
789+
# per-row weights (weights were already applied)
790+
return pd.DataFrame(results) if results else pd.DataFrame()
795791

796792
return fn
797793

@@ -850,10 +846,12 @@ def fn(*args, **kwargs):
850846
)(*args, **kwargs)
851847
except Exception:
852848
pass
849+
# Return plain DataFrame - aggregated results don't
850+
# have per-row weights (weights were already applied)
853851
return (
854-
MicroDataFrame(results)
852+
pd.DataFrame(results)
855853
if results
856-
else MicroDataFrame()
854+
else pd.DataFrame()
857855
)
858856

859857
return fn
@@ -871,10 +869,12 @@ def fn(*args, **kwargs):
871869
)(*args, **kwargs)
872870
except Exception:
873871
pass
872+
# Return plain DataFrame - aggregated results don't
873+
# have per-row weights (weights were already applied)
874874
return (
875-
MicroDataFrame(results)
875+
pd.DataFrame(results)
876876
if results
877-
else MicroDataFrame()
877+
else pd.DataFrame()
878878
)
879879

880880
return fn

microdf/tests/test_microseries_dataframe.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,8 @@ def test_groupby_column_selection() -> None:
339339
result_list = d.groupby("g")[["y"]].sum()
340340
assert result_list.loc["a", "y"] == 14.0
341341
assert result_list.loc["b", "y"] == 18.0
342+
343+
# Aggregated results should be plain DataFrame (no spurious weight column)
344+
result_all = d.groupby("g").sum()
345+
assert "weight" not in result_all.columns
346+
assert list(result_all.columns) == ["y"]

0 commit comments

Comments
 (0)