-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: .groupby() produces MultiIndex with NaN in levels instead of encoded in codes #54347
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
I'm not that familiar with the code base so I can't tell if the following is the correct solultion, but a possible fix is to call pandas/pandas/core/groupby/ops.py Lines 766 to 775 in 8067a01
This results in the codes being verifed and NaN encodeded as -1 pandas/pandas/core/indexes/multi.py Lines 361 to 363 in 8067a01
The above might not be desirable due to performance overhead. Another solution is to override the codes just before they are passed to pandas/pandas/core/indexes/multi.py Lines 370 to 392 in 8067a01
|
cc @rhshadrach this does look buggy to me |
I'm not able to reproduce on main. The OP example does not raise. Agreed that if it does raise, there is a bug. |
Oh good point, looks like I tried on 2.0.3 |
Starting work on this, will try to implement the test. |
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 snippet above results in an AssertionError, because NaN in the MultiIndex is encoded in levels when returned from
.groupby()
, but in codes when returned frompd.MultiIndex.from_frame()
.See
pandas/pandas/core/groupby/grouper.py
Lines 767 to 779 in 4357621
Expected Behavior
I expect the assertion to pass.
Installed Versions
INSTALLED VERSIONS
commit : 0f43794
python : 3.11.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 2.0.3
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2
Cython : None
pytest : 7.4.0
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
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : 2.0.19
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
Process finished with exit code 0
The text was updated successfully, but these errors were encountered: