Skip to content

pivot_table passes junk to aggfunc when value column does not exist #10326

Closed
@edparcell

Description

@edparcell

Expected behavior is that pivot_table would throw an exception explaining the missing column. Instead pivot_table passes DataFrames to aggfunc before checking.

Example:

def agg(l):
    it = iter(l)
    try:
        value = next(it)
    except StopIteration:
        raise Exception("0 items in iterator")
    try:
        next(it)
        raise Exception("More than 1 item in iterator")
    except StopIteration:
        return value

foo = pd.DataFrame({"X": [0, 0, 1, 1], "Y": [0, 1, 0, 1], "Z": [10, 20, 30, 40]})
foo.pivot_table('Z', 'X', 'Y', aggfunc=agg)

gives, as expected:

Y   0   1
X        
0  10  20
1  30  40

But the following throws an Exception from agg, rather than an exception from pivot_table about the missing value column:

foo.pivot_table('notpresent', 'X', 'Y', aggfunc=agg)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions