BUG: `TypeError: Int64` when using `pd.pivot_table(..., margins=True)` with Int64 values

This issue has been created since 2022-09-21.

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(
    {
        "state": ["CA", "WA", "CO", "AZ"] * 3,
        "office_id": list(range(1, 7)) * 2,
        "sales": [np.random.randint(100_000, 999_999) for _ in range(12)]
    }
).astype(
    {
        "sales": pd.Int64Dtype()
    }
)

df.pivot_table(index="office_id", columns="state", margins=True, aggfunc="sum")

Issue Description

This issue looks similar to this one, however I have confirmed the issue is still present on 1.5.0. The difference here is the addition of margins=True.

Expected Behavior

import pandas as pd
import numpy as np


df = pd.DataFrame(
    {
        "state": ["CA", "WA", "CO", "AZ"] * 3,
        "office_id": list(range(1, 7)) * 2,
        "sales": [np.random.randint(100_000, 999_999) for _ in range(12)]
    }
).astype(
    {
        "sales": np.int64  # works
    }
)

df.pivot_table(index="office_id", columns="state", margins=True, aggfunc="sum")

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 1.5.0
numpy : 1.23.1
pytz : 2022.1
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 9.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-09-29
Star Count 35374
Watcher Count 1122
Fork Count 15034
Issue Count 3579

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Change scss from .Permalink to .RelPermalink 1 2022-07-25 2022-07-26
`Any` doesn't work with IRedisCollection 0 2022-01-12 2022-09-03
[BUG] Some mp3 files report 1/2 of actual duration 2 2022-05-19 2022-08-30
(0.9.0) App is Crashing on App Store / Test Flight 0 2022-04-29 2022-09-14
0.9.0 Not working with Alamofire 1 2022-05-11 2022-09-14
must be used from main thread only 0 2022-07-29 2022-09-14
feature: copy button for request and response body 0 2022-04-25 2022-09-14
create smpls for custom dataset 0 2021-02-03 2022-09-21
VASP6 with VASPsol add-on - compile error 4 2020-08-13 2022-09-10
RHOION file and RHOB file 5 2020-06-24 2022-09-10
Applied external potential on the adsorption and surface diffusion for metal electrodes. 2 2019-08-29 2022-09-10
Optimize NCCL collectives 0 2021-02-23 2022-09-27
[Feature] Add context menu option to only scan for log4j 3 2021-12-15 2022-09-28
Payloads are not sent with the plugin 10 2022-01-21 2022-09-25
Generate scaladoc of unstable 12 2022-07-04 2022-08-09
ErrorException in GET /SoporteHACR/public/auth/register 0 2022-05-19 2022-09-11
Power button unresponsive 3 2021-12-16 2022-08-09
[minor] UNIX games path (Filesystem Hierarchy Standard) 0 2022-09-17 2022-09-27
SaveBody now uses filename from content-disposition 1 2021-11-14 2022-09-21
5.2.0 Cannot execute on Linux 1 2022-05-23 2022-08-19
smlparser.cpp error in handling lat and lon min/max 1 2021-05-29 2022-09-27
Custom modules appear in Studio but nowhere else 4 2022-01-17 2022-09-09
two questions regarding the useContextUpdate hook 3 2021-09-21 2022-09-21
Warn for conditional function definition in local scopes 0 2020-11-19 2022-09-07
Dockerize deployment 13 2020-01-10 2022-07-06
Search configuration 7 2020-11-06 2022-07-16
Searchable operators 0 2020-03-06 2021-12-19
Matlab crash when using calcDensity to produce pole figure 2 2022-06-21 2022-09-20
Python requirements problem on Ubuntu 20.04 2 2021-05-18 2022-09-27
Problem with nerdctl save + load: content digest not found 4 2021-10-03 2022-07-09
On error occured, why not wait for unfinished targets to finish. 2 2021-06-28 2022-09-02
Update default EE when the EE has USER 1000 removed 0 2021-08-03 2022-09-27
DataTables warning: table id=subdomain_scan_results - Ajax error 1 2022-09-22 2022-09-18
XRDP on Raspberry Pi 3A+ Very Slow/Not Connecting to Windows 10 Remote Desktop 2 2022-07-04 2022-09-18
xRDP + XFCe4 = no shutdown/reboot buttons 6 2022-07-01 2022-09-11
Refresh issue: recent changes are not shown but can be staged 1 2022-08-19 2022-09-13
I use port 443 to connect to mqtt of aws, but I cannot connect successfully. 5 2021-08-12 2022-09-26
Unknown option --resolver in pip-compile for python3.6 + piptools-6.4.0 0 2022-07-21 2022-08-07
cmd/compile: build failure with unified IR ("no field or method") 4 2021-11-12 2022-07-28
[WIP] (feat) machine-specific frontend 0 2019-05-29 2022-02-06
donnemartin/system-design-primer 0 2021-03-29 2022-08-10
[BUG] MultiModalPredictor crashes in best_quality mode 2 2022-07-21 2022-09-05
Bug in reference conv3d 2 2021-05-27 2022-09-09
Installing CDM ? 1 2019-07-15 2022-01-11
[YSQL] prepared statement with bigint paramType on integer column table takes significantly more time compared to postgres 6 2022-06-21 2022-08-06
I can no longer update my home assistant core 2022.4.1 16 2022-04-16 2022-09-27
[Video Conference Mute] Flyout is extremely small 1 2022-04-09 2022-09-04
ModuleNotFoundError: No module named 'anonymization.lib' 7 2021-03-20 2022-09-22
Signing request Sciebo RDS 1 2020-07-14 2022-09-17
Java errors/warnings have no column position info 0 2020-10-27 2022-09-06