BUG: Wrong grouping of categoricals when observed=True · Issue #21151 · pandas-dev/pandas (original) (raw)
Setup:
s1 = pd.Categorical([np.nan, 'a', np.nan, 'a'], categories=['a', 'b']) s2 = pd.Series([1, 2, 3, 4]) df = pd.DataFrame({'s1': s1, 's2': s2})
Comparing results with observed=False
and observed=True
:
df.groupby('s1').sum() # ok s2 s1 a 6 b 0 df.groupby('s1', observed=True).sum() s2 s1 NaN 6 # should not be shown a 0 # should be 6
Notice the value are assigned wrongly.
Also, notice that NaN is now a possible label.
If the first value is not a Nan, the assignment works fine (but Nan is still a possible label)
df[1:].groupby('s1', observed=True).sum() s2 s1 a 6 NaN 0
Problem description
The problem concerns when there are unobserved labels and the first value is Nan. If there are no unobserved values, everyting seems alright from my checks.
Nan should probably not be a possible label.
Expected Output
I would assume same output as when òbserved=False
, but without unobserved labels:
df.groupby('s1', observed=True).sum() s2 s1 a 6
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 32
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.23.0
pytest: 3.4.0
pip: 10.0.1
setuptools: 38.4.1
Cython: 0.26.1
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.3.1
sphinx: 1.7.4
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.8
xlrd: None
xlwt: None
xlsxwriter: None
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None