Skip to content

pivot_table with margins=True shows NaN with Period columns #28323

Closed
@lucatrv

Description

@lucatrv

The following code reproduces the issue:

import pandas as pd

df = pd.DataFrame(
    {
        "a": [1, 1, 2, 2],
        "b": [
            pd.Period("2019Q1"),
            pd.Period("2019Q2"),
            pd.Period("2019Q1"),
            pd.Period("2019Q2"),
        ],
        "x": 1.0,
    }
)

df.pivot_table(index="a", columns="b", values="x", margins=True)

Output:

b 	2019Q1 	2019Q2 	All
a 			
1 	1.0 	1.0 	1.0
2 	1.0 	1.0 	1.0
All	NaN 	NaN 	1.0

Expected output:

b 	2019Q1 	2019Q2 	All
a 			
1 	1.0 	1.0 	1.0
2 	1.0 	1.0 	1.0
All	1.0 	1.0 	1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions