Skip to content

BUG: margin for pivot_table is incorrect with NA column/index values #61509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
vtraag opened this issue May 28, 2025 · 2 comments · May be fixed by #61524
Open
3 tasks done

BUG: margin for pivot_table is incorrect with NA column/index values #61509

vtraag opened this issue May 28, 2025 · 2 comments · May be fixed by #61524
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@vtraag
Copy link

vtraag commented May 28, 2025

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

df = pd.DataFrame({"i": [1, 2, 3],
                   "g1": ["a", "b", "b"],
                   "g2": ["x", None, None],
                   })

df.pivot_table(index="g1",
               columns="g2",
               values="i",
               aggfunc="count",
               dropna=False, margins=True)

Issue Description

The margins of a pivot_table are incorrect when the index or columns variables contains missing variables. In particular, for the variable with the missing value, the total is missing.

g1 x nan Total
a 1.0 1
b 2.0 2
All 1.0 3

Expected Behavior

Margins should also be included for columns/indices with missing values.

In particular, the table should look like this

g1 x nan Total
a 1.0 1
b 2.0 2
All 1.0 2.0 3

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.12.8.final.0
python-bits : 64
OS : Linux
OS-release : 6.11.0-26-generic
Version : #26~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr 17 19:20:47 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.4
pytz : 2025.1
dateutil : 2.9.0.post0
setuptools : 75.8.0
pip : 25.0
Cython : None
pytest : None
hypothesis : None
sphinx : 8.1.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.5
IPython : 8.22.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.0
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.9.4
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.4
pandas_gbq : None
pyarrow : 16.1.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.23.0
tzdata : 2025.1
qtpy : 2.4.2
pyqt5 : None

@vtraag vtraag added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 28, 2025
@rhshadrach
Copy link
Member

Thanks for the report. Confirmed on main, further investigations and a PR to fix is welcome!

Related: #53521

@rhshadrach rhshadrach added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 30, 2025
@iabhi4
Copy link
Contributor

iabhi4 commented May 31, 2025

Thanks for the report. Confirmed on main, further investigations and a PR to fix is welcome!

Hi @rhshadrach just raised a PR for this

It ensures pivot_table(..., margins=True, dropna=False) properly includes rows/columns with NA values in the margin computation. Let me know if there's anything you'd like adjusted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants