Skip to content

BUG: pd.pivot_table margins don't include rows, which have a NaN within any column, for all columns #47447

Closed
@xArbisRox

Description

@xArbisRox

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(index1=["a", "b", "c", "b", "a"], index2=["a", "b", "c", "b", "a"], value1=[10, 11, 12, 13, 14], value2=[15, 16, 17, 18, 19], valueNaN=[np.nan, 100, np.nan, 100, np.nan]))
print(df)
piv = pd.pivot_table(df, values=["value1", "value2", "valueNaN"], index=["index1", "index2"], aggfunc='sum', margins=True)
print(piv)

Issue Description

df:

  index1 index2  value1  value2  valueNaN
0      a      a      10      15       NaN
1      b      b      11      16     100.0
2      c      c      12      17       NaN
3      b      b      13      18     100.0
4      a      a      14      19       NaN

Creating a pivot table with margins on multiple value columns within one function call, leads to the effect that the margins do not include rows, which have a NaN at any column, for all columns.
The above example leads to margins 24 and 34 for columns value1 and value 2

               value1  value2  valueNaN
index1 index2                          
a      a           24      34       0.0
b      b           24      34     200.0
c      c           12      17       0.0
All                24      34     200.0

Expected Behavior

Expectation is that the margins include all values of columns value1 and value2, i.e. 24+24+12=60 and 34+34+17=85, instead of merely displaying the figures from index ('b', 'b'), i.e. 24 and 34, just because the other rows include NaN within the column valueNaN.
Expected results occur, when filling the NAs before creating the pivot:
fixed_df = df.fillna(value=0)

  index1 index2  value1  value2  valueNaN
0      a      a      10      15       0.0
1      b      b      11      16     100.0
2      c      c      12      17       0.0
3      b      b      13      18     100.0
4      a      a      14      19       0.0

fixed_piv = pd.pivot_table(fixed_df, values=["value1", "value2", "valueNaN"], index=["index1", "index2"], aggfunc='sum', margins=True)

               value1  value2  valueNaN
index1 index2                          
a      a           24      34       0.0
b      b           24      34     200.0
c      c           12      17       0.0
All                60      85     200.0

However, the current behavior, to not include rows with a NaN at all should not be default - or at least an appropriate information / warning should be provided for the pivot_table functionality.

Installed Versions

pd.show_versions()
INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.9.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : AMD64 Family 23 Model 8 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.4.2
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.3.4
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

Labels

DocsReshapingConcat, Merge/Join, Stack/Unstack, Explode

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions