Skip to content

BUG: pd.crosstab(dropna=True, values=) drops rows and columns where any aggregation result is null. #60767

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
2 of 3 tasks
sfc-gh-mvashishtha opened this issue Jan 22, 2025 · 1 comment
Labels
Bug Needs Discussion Requires discussion from core team before further action Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@sfc-gh-mvashishtha
Copy link

sfc-gh-mvashishtha commented Jan 22, 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(
    {"A": [1, 1, 2, 2, 2], "B": [3, 3, 4, 4, 4], "C": [1, 1, 1, 1, 1]},
    dtype=float    
)

assert (
    pd.crosstab(index=df["A"], columns=df["B"], values=df["C"], aggfunc="skew", dropna=True).shape == 
    pd.crosstab(index=df["A"], columns=df["B"], values=df["C"], aggfunc="skew", dropna=False).shape
)

Issue Description

When I provide dropna=True to crosstab and specify a values array to aggregate, pandas seems to drop rows and columns where all the results are NaN. In this case pandas skew gives NaN if there are fewer than 3 values in a group, so only the value for index=2, column=4 has a non-NaN value. That leaves a row of NaN and a column of NaN, so pandas drops both the all-NaN row and the all-NaN column.

Expected Behavior

The documentation says that dropna=True means, "Do not include columns whose entries are all NaN." I expect pandas to follow that rule and to keep values where the aggregation result is NaN. In the example above, I expect to see 2 rows for the 2 possible values of A, 1 and 2, an 2 columns for the 2 possible values of B, 3 and 4.

Installed Versions

INSTALLED VERSIONS
------------------
commit                : 0691c5cf90477d3503834d983f69350f250a6ff7
python                : 3.9.21
python-bits           : 64
OS                    : Darwin
OS-release            : 24.2.0
Version               : Darwin Kernel Version 24.2.0: Fri Dec  6 18:56:34 PST 2024; root:xnu-11215.61.5~2/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.3
numpy                 : 2.0.2
pytz                  : 2024.2
dateutil              : 2.8.2
pip                   : 24.2
Cython                : None
sphinx                : None
IPython               : 8.12.0
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : None
blosc                 : None
bottleneck            : None
dataframe-api-compat  : None
fastparquet           : None
fsspec                : None
html5lib              : None
hypothesis            : None
gcsfs                 : None
jinja2                : None
lxml.etree            : None
matplotlib            : None
numba                 : None
numexpr               : None
odfpy                 : None
openpyxl              : None
pandas_gbq            : None
psycopg2              : None
pymysql               : None
pyarrow               : None
pyreadstat            : None
pytest                : None
python-calamine       : None
pyxlsb                : None
s3fs                  : None
scipy                 : None
sqlalchemy            : None
tables                : None
tabulate              : None
xarray                : None
xlrd                  : None
xlsxwriter            : None
zstandard             : None
tzdata                : 2025.1
qtpy                  : None
pyqt5                 : None
@rhshadrach
Copy link
Member

Thanks for the report.

In the example above, I expect to see 2 rows for the 2 possible values of A, 1 and 2, an 2 columns for the 2 possible values of B, 3 and 4.

When dropna=False, there is a column of all NaN values. Why should this not be dropped?

In regards to the dropping of the row, I expect this has some overlap with #53521, but haven't checked this.

@rhshadrach rhshadrach added Needs Discussion Requires discussion from core team before further action Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Discussion Requires discussion from core team before further action Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants