Skip to content

Commit

Permalink
fix for slightly broken pivotplugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzoic committed Sep 24, 2024
1 parent e4fe182 commit 1e6d397
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions countess/plugins/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,22 @@ def finalize(self) -> Iterable[pd.DataFrame]:
# series, and then `.agg()` applies the appropriate aggregation function.
# XXX `mean` could get streamlined by recording (sum, count) tuples at the first stage,
# and then adding those up here and dividing (but it's a bit tricky)

def _aggfunc():
if self.aggfunc in ("min", "max", "sum"):
return self.aggfunc.value
return lambda s: s.explode().agg(self.aggfunc.value)

dataframe = pd.concat(self.dataframes)
index_cols = [p.label for p in self.columns if p.value == "Index"]
try:
dataframe = pd.concat(self.dataframes)
columns = get_all_columns(dataframe)
index_cols = [p.label for p in self.columns if p.value == "Index" and p.label in columns]

if index_cols:
dataframe = dataframe.groupby(by=index_cols, group_keys=True).agg(_aggfunc())

if index_cols:
dataframe = dataframe.groupby(by=index_cols, group_keys=True).agg(_aggfunc())
yield dataframe
except (KeyError, ValueError, TypeError) as exc:
logger.warning("Exception", exc_info=exc)

yield dataframe
yield from super().finalize()

0 comments on commit 1e6d397

Please sign in to comment.