Skip to content

pandas pivot_table aggfunc/values parameters behaving oddly #29080

Closed
@it176131

Description

@it176131
np.random.seed(0)

test = pd.DataFrame({
    'a' : np.random.randint(0, 10, size=(10,)),
    'b' : np.random.randint(0, 10, size=(10,)),
    'c' : np.random.randint(0, 10, size=(10,)),
    'd' : np.random.randint(0, 10, size=(10,)),
})

tp = test.pivot_table(index=[
    'a',
], columns=[
    'b',
], values=[
    'c',
], aggfunc=[
    'nunique'
])

# tp produces unexpected output
print(tp)

  nunique                                                       
        a                   b                   c               
b       1    6    7    8    1    6    7    8    1    6    7    8
a                                                               
0     NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN
2     NaN  NaN  NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN  1.0
3     NaN  NaN  1.0  1.0  NaN  NaN  1.0  1.0  NaN  NaN  1.0  2.0
4     1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN
5     NaN  NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN  2.0  NaN
7     1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN
9     NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN  NaN  1.0  NaN  NaN

tp2 = test.pivot_table(index=[
    'a',
], columns=[
    'b',
], values=[
    'c',
], aggfunc=[
    pd.Series.nunique
])

# tp2 produces expected output
print(tp2)

  nunique               
        c               
b       1    6    7    8
a                       
0     NaN  1.0  NaN  NaN
2     NaN  NaN  NaN  1.0
3     NaN  NaN  1.0  2.0
4     1.0  NaN  NaN  NaN
5     NaN  NaN  2.0  NaN
7     1.0  NaN  NaN  NaN
9     NaN  1.0  NaN  NaN

cp = test.pivot_table(index=[
    'a',
], columns=[
    'b',
], values=[
    'c',
], aggfunc=[
    'count'
])

cp2 = test.pivot_table(index=[
    'a',
], columns=[
    'b',
], values=[
    'c',
], aggfunc=[
    pd.Series.count
])

# both return the same output
  count               
      c               
b     1    6    7    8
a                     
0   NaN  1.0  NaN  NaN
2   NaN  NaN  NaN  1.0
3   NaN  NaN  1.0  2.0
4   1.0  NaN  NaN  NaN
5   NaN  NaN  2.0  NaN
7   1.0  NaN  NaN  NaN
9   NaN  1.0  NaN  NaN

Problem description

I noticed that when using pd.pivot_table I get different results specifically when using aggfunc='nunique' and values = a single column vs values = a list of columns.

When aggfunc='count', there is no difference between outputs.

Please see my stackoverflow question here for more details/examples. They mentioned in the comments that I should log an issue

Expected Output

I would expect both tp and tp2 to produce the same output

  nunique               
        c               
b       1    6    7    8
a                       
0     NaN  1.0  NaN  NaN
2     NaN  NaN  NaN  1.0
3     NaN  NaN  1.0  2.0
4     1.0  NaN  NaN  NaN
5     NaN  NaN  2.0  NaN
7     1.0  NaN  NaN  NaN
9     NaN  1.0  NaN  NaN

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.1
numpy : 1.16.5
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.3
setuptools : 41.2.0
Cython : 0.29.13
pytest : 5.1.2
hypothesis : None
sphinx : 2.2.0
blosc : None
feather : None
xlsxwriter : 1.2.1
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.8.0
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : 2.7.0
odfpy : None
openpyxl : 2.6.3
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : 1.3.8
tables : 3.5.2
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.1

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions