-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: pandas pivot_table count over a dataframe with 2 columns results in empty dataset when using aggfunc="count" #57876
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Comments
When I read the documentation of I guess probably this is not what you are looking for, but here is one way to avoid empty dataframe. import pandas as pd
data = {'Category': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],
'Value': [10, 20, 30, 40, 50, 60, 70, 80, 90]}
df = pd.DataFrame(data)
df["num"] = 1
pivot_table = df.pivot_table(index='Category', columns='Value', values='num', aggfunc="count")
print(pivot_table) |
Thanks for the folllow up @yukikitayama Maybe the solution is what @e-motta suggests in this Stackoverflow comment? |
- Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue pandas-dev#57876.
- Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue pandas-dev#57876.
- Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue pandas-dev#57876.
- Added pivot_table bug to Bugs/Reshaping section referencing issues pandas-dev#57876 and pandas-dev#61292.
- Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue pandas-dev#57876.
- Added pivot_table bug to Bugs/Reshaping section referencing issues pandas-dev#57876 and pandas-dev#61292.
* modified: pandas/tests/reshape/test_pivot_multilevel.py - Added two tests, :func:`test_pivot_table_values_in_columns` and :func:`test_pivot_table_values_in_index`, to ensure that the `values` param is still used when the argument is shared between the `columns` and `values` params, and `index` and `values` params. * modified: pandas/core/reshape/pivot.py - Added condition to :func:`__internal_pivot_table` to aggregate `values` explicitly if `values` were passed, otherwise aggregate all remaining columns. This allows the tests :func:`test_pivot_table_values_in_columns` and :func:`test_pivot_table_values_in_index` in test_pivot_multilevel.py to pass. * modified: pandas/tests/reshape/test_pivot.py - Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue #57876. * modified: pandas/tests/reshape/test_pivot.py - Added GH issue comment to test :func:`test_pivot_table_values_as_two_params`. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Combined tests :func:`test_pivot_table_values_in_columns` and :func:`test_pivot_table_values_in_index` into a single parametrized test, :func:`test_pivot_table_multiindex_values_as_two_params` to reduce duplicate setup code. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Added GH issue #61292 as comment to test :func:`test_pivot_table_multiindex_values_as_two_params`. * modified: pandas/core/reshape/pivot.py - Simplified proposed logic in :func:`__internal_pivot_table`. * modified: pandas/core/reshape/pivot.py - Added GH issue numbers to new logic in :func:`__internal_pivot_table`. * modified: pandas/core/reshape/pivot.py - Added ignore-comment to silence mypy error in :func:`__internal_pivot_table`. - Added TODO-comment stating that the :meth:`DataFrameGroupBy.__getitem__` should be overloaded to match the pandas-stubs type declarations, informing mypy that the type is correct given `values` is a list. * modified: doc/source/whatsnew/v3.0.0.rst - Added pivot_table bug to Bugs/Reshaping section referencing issues #57876 and #61292. * modified: pandas/core/reshape/pivot.py - Moved and simplified mypy comment per feedback. * modified: pandas/core/reshape/pivot.py - Removed comment about explicit aggregation per feedback. * modified: pandas/tests/reshape/test_pivot.py - Removed param names and updated `argnames` arg per feedback in parametrized marker. * modified: pandas/tests/reshape/test_pivot.py - Removed param names in favor of implicit args per feedback. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Removed param names and updated arg for `argnames` in parametrized marker per feedback. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Reduced `expected` assignments from two to one per feedback. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Removed param names in favor of implicit args per feedback. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Moved e_data, e_index, and e_cols to parametrized marker instead of declaring inside the test :func:`test_pivot_table_multiindex_values_as_two_params`. * modified: pandas/tests/reshape/test_pivot.py - Moved `expected` setup to parametrized marker instead of in the test :meth:`TestPivotTable.test_pivot_table_values_as_two_params`. * modified: pandas/tests/reshape/test_pivot.py - Removed walrus operator declarations in parametrized marker for test :meth:TestPivotTable.test_pivot_table_values_as_two_params`. Appears related to this mypy issue -> python/mypy#17377. * modified: pandas/tests/reshape/test_pivot_multilevel.py - Removed walrus operator declarations as I'm sure mypy would raise an issue with it given that it did in test_pivot.py (see commit 52cf560).
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
Run:
and you will get:
Change to
and you will get:
Issue Description
Try to use df.pivot_table over a dataframe with 2 columns, using same column name for the columns and values paramete, aggregate using "count" gets you an empty dataset. Switch to len or size and you get the right result.
Furthermore, strangely, this workaround somehow fixes "count":
ValueCopy and Value contain identical data, so they should be interchangeable, that is, using either should lead to the same result, but they do not.
For sanity checking I tried the same pivoting in Polars, and there, count works fine:
results in:
Likewise with DuckDb:
it works out all-right:
Expected Behavior
pivot_table = df.pivot_table(index='Category', columns='Value', values='Value', aggfunc="count")
and
pivot_table = df.pivot_table(index='Category', columns='Value', values='Value', aggfunc="size")
and
pivot_table = df.pivot_table(index='Category', columns='Value', values='Value', aggfunc=len)
should all produce an non empty dataset like the one below (but
count
somehow fails and produces an empty one):Installed Versions
INSTALLED VERSIONS
commit : bdc79c1
python : 3.10.13.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-1015-gcp
Version : #15~22.04.1-Ubuntu SMP Wed Feb 14 21:22:00 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.0.2.post0
pip : 23.3.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 : None
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 : None
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: