Skip to content

wrong aggregated results when pivot_table index is ordered categorical data  #25815

Closed
@peterpanmj

Description

@peterpanmj

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

_c = np.arange(5,0,-1)

df = pd.DataFrame(np.stack([_c]*3).T, columns=['A', 'B', 'C'])

df["D"] = pd.Series(["A","B","B","A", "A"])

df["E"] = pd.cut(x=df["A"],bins=[0,3,6,10])

print(pd.pivot_table(df, index='E', columns=['D'], values="B", aggfunc="sum", margins=True))
Output is

D       A  B  All
E                
(0, 3]  3  3    9
(3, 6]  5  4    6
All     8  7   15

Problem description

the data is as follows:

	A	B	C	D	E
0	5	5	5	A	(3, 6]
1	4	4	4	B	(3, 6]
2	3	3	3	B	(0, 3]
3	2	2	2	A	(0, 3]
4	1	1	1	A	(0, 3]

Margin calculation results are in wrong order
(0, 3] should have a sum value of 6,
(3, 6] should have a sum value of 9

If I dont use column argument, results are correct.

B
E	
(0, 3]	6
(3, 6]	9
(6, 10]	0
All	15

I've tried both .23.4 and .24.2 . They have the same results.

Expected Output

D       A  B  All
E                
(0, 3]  3  3    6
(3, 6]  5  4    9
All     8  7   15

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: zh_CN.GBK@cjknarrow
LOCALE: None.None

pandas: 0.23.4
pytest: 4.0.2
pip: 18.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeNeeds TestsUnit test(s) needed to prevent regressionsReshapingConcat, Merge/Join, Stack/Unstack, Explodegood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions