-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
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.
To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Reproducible Example
# With pandas 1.5
from pandas import DataFrame, Categorical
df = DataFrame({"x": Categorical([1, 2], categories=[1, 2, 3]), "y": [3, 4]})
df.groupby("x", observed=False).grouper.result_index
# CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=False, dtype='category', name='x')
df.groupby("x", observed=False, dropna=False).grouper.result_index
# CategoricalIndex([1, 2], categories=[1, 2, 3], ordered=False, dtype='category', name='x')
# ------------------------------------------------------------------------------------------
# Unexpected result ↑
df.groupby("x", observed=False, dropna=True).grouper.result_index
# CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=False, dtype='category', name='x')
# With pandas 1.4.4 and prior
df.groupby("x", observed=False).grouper.result_index
# CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=False, dtype='category', name='x')
df.groupby("x", observed=False, dropna=False).grouper.result_index
# CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=False, dtype='category', name='x')
df.groupby("x", observed=False, dropna=True).grouper.result_index
# CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=False, dtype='category', name='x')
Issue Description
dropna=False
in DataFrame.groupby()
should not affect the results when observed=False
.
Expected Behavior
Expected the behavior with pandas 1.4.4 and prior.
Installed Versions
INSTALLED VERSIONS
commit : 87cfe4e
python : 3.9.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.57.1-microsoft-standard-WSL2
Version : #1 SMP Wed Jul 27 02:20:31 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 58.0.0
pip : 22.2.2
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.1.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
brotli :
fastparquet : None
fsspec : 2022.02.0
gcsfs : None
matplotlib : 3.5.1
numba : 0.53.1
numexpr : None
odfpy : None
openpyxl : 3.0.8
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.28
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
Activity
phofl commentedon Sep 19, 2022
cc @rhshadrach
Confirmed:
#46601
rhshadrach commentedon Sep 20, 2022
#46601 fixed an issue with dropna and categorical, namely dropna with categorical still drops null values. On 1.4.x:
The null value is included in the result on 1.5.0. As identified, the patch did not correctly implement the case where
observed=False
.I've looked into this, and it appears to me our current implementation of categorical with nulls and dropna are incompatible in groupby. Namely, categorical encodes values as nonnegative integers with nulls being represented by -1 while groupby with
dropna=False
requires nulls be encoded by nonnegative integers.We could maybe hack in a patch where we add the null value(s?) to the categories only to remove them upon returning the result. This seems like it would be too significant of a change for a patch release, fragile, and prone to bugs. I am wondering if a better direction I think would be to reimplement groupby so that negative codes are only dropped when
dropna=True
. This may have some drawbacks and would need some experimenting, but again, too large of a change for a patch version in my opinion.With this, my recommendation is to undo the offending line from #46601, i.e. change
pandas/pandas/core/groupby/grouper.py
Line 663 in 73d15a7
to become
if self._passed_categorical:
. This would make it so thatdropna=False
does not work with categorical again, but fixing this regression. I will put up a PR for this, but wanted to see if others have any thoughts first.cc @jbrockmendel @mroeschke @jreback @phofl @jorisvandenbossche