-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: margins
value incorrect with count
aggfunc and no index
#58722
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
Comments
It appears to me the current behavior is how it's documented. https://pandas.pydata.org/docs/reference/api/pandas.pivot_table.html
You seem to think pandas should always use |
This comment was marked as off-topic.
This comment was marked as off-topic.
@rhshadrach to clarify my question was more on whether the inputs to the aggfunc in the margin were the original values, or the aggregated values. For context, take the following dataframe: A B C D E F
0 foo baz dull 0 0.0 None
1 foo baz dull 1 1.0 None
2 foo baz shiny 2 2.0 None
3 foo baz dull 3 3.0 None
4 bar baz dull 4 NaN None
5 bar baz shiny 5 5.0 None
6 bar baz shiny 6 6.0 None
7 bar baz dull 7 7.0 None
8 foo baz dull 8 8.0 None
9 foo baz shiny 9 NaN None
10 foo buz shiny 10 10.0 None
11 foo buz shiny 11 NaN None
12 foo baz spot 12 NaN None
13 bar baz spot 13 NaN None if we call In [26]: pivot_kwargs
Out[26]:
{'index': None,
'columns': ['B', 'C'],
'values': 'D',
'dropna': False,
'fill_value': None,
'margins': True}
In [27]: df.pivot_table(**pivot_kwargs)
Out[27]:
B baz buz
C dull shiny spot All dull shiny spot All
D 3.833333 5.5 12.5 7.277778 NaN 10.5 NaN 10.5 We get 7.28 for the margins value of In [28]: pivot_kwargs['columns'] = 'B'
In [29]: df.pivot_table(**pivot_kwargs)
Out[29]:
B baz All buz All
D 5.833333 5.833333 10.5 10.5 We get 5.83 for the aggregate value of In [40]: pivot_kwargs
Out[40]:
{'index': 'A',
'columns': ['B', 'C'],
'values': 'D',
'dropna': False,
'fill_value': None,
'margins': True}
In [41]: df.to_pandas().pivot_table(**pivot_kwargs)
Out[41]:
B baz buz All
C dull shiny spot dull shiny spot
A
bar 5.500000 5.5 13.0 NaN NaN NaN 7.000000
foo 3.000000 5.5 12.0 NaN 10.5 NaN 6.222222
All 3.833333 5.5 12.5 NaN 10.5 NaN 6.500000 if we follow the aggregation over aggregation rule from above, the margin for |
Thanks - agree there is an inconsistency here. Am I correct in saying that the OP example does not demonstrate the issue? it would be helpful to post reproducible and minimal examples. Can you update the OP? I'd recommend something like this: df = pd.DataFrame({'x': [1, 1, 2], 'y': [3, 3, 4], 'z': [5, 5, 6], 'w': [7, 8, 9]})
print(df.pivot_table(columns=["y", "z"], values="w", margins=True, aggfunc="count"))
# y 3 4
# z 5 All 6 All
# w 2 1 1 1
print(df.pivot_table(index="x", columns=["y", "z"], values="w", margins=True, aggfunc="count"))
# y 3 4 All
# z 5 6
# x
# 1 2.0 NaN 2
# 2 NaN 1.0 1
# All 2.0 1.0 3 |
It appears to me that the
|
Thank you @rhshadrach for confirming - I'll go ahead and update the OP with the example you suggest! |
take |
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
Issue Description
The all column should be a subaggregation over the aggregations, but seems to perhaps be calling
count
on the result rather than aggregating the counts? E.g., the first result should look like this:Expected Behavior
Values should be aggregations computed over the original data pivoting only on the first pivot column.
Installed Versions
INSTALLED VERSIONS
commit : d9cdd2e
python : 3.11.5.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 68.0.0
pip : 23.2.1
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 : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.19.0
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: