Skip to content

pandas/pivot-table-default-mean

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

pivot_table without aggfunc takes the mean of the rows that share a cell and says nothing, so two rows of 10 and 30 become 20.0. Name the aggregation, or call pivot, which raises when a cell has more than one row.

table = df.pivot_table(index="city", columns="year", values="sold")
table = df.pivot_table(index="city", columns="year", values="sold", aggfunc="sum")

The rule reports pivot_table without aggfunc, which takes the mean of the rows that share a cell without a warning. The table shows the cell for two rows of 10 and 30 with one city and one year.

Call Cell
pivot_table without aggfunc 20.0
pivot_table(aggfunc="sum") 40
pivot ValueError "Index contains duplicate entries, cannot reshape"

With --unsafe-fixes, xpd makes the mean explicit with aggfunc="mean". Change it if you intended another aggregation.

Write aggfunc="mean" when you want the mean.

pivot_table without aggfunc takes the mean of the rows that share a cell and says nothing. Two rows of 10 and 30 came back as 20.0. Name the aggregation, as in aggfunc="sum" or aggfunc="mean", or call pivot, which raises ValueError when a cell has more than one row.

See all rules in the pandas pack.