-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: Wrong grouping of categoricals when observed=True #21151
Comments
it seems to be related to issue #21133 |
I've also tested the above 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})
>>> df
s1 s2
0 NaN 1
1 a 2
2 NaN 3
3 a 4 Comparing results with >>> df.groupby('s1').first()
s2
s1
a 2.0
b NaN
>>> df.groupby('s1', observed=True).first()
s2
s1
NaN 2.0
a NaN When
Output of
|
In [9]: import pandas as pd
...: s1 = pd.Categorical([np.nan, 'a', np.nan, 'a'], categories=['a', 'b','c'])
...: s2 = pd.Series([1,2,3,4])
...: df = pd.DataFrame({'s1':s1, 's2':s2})
In [10]: df
Out[10]:
s1 s2
0 NaN 1
1 a 2
2 NaN 3
3 a 4
In [11]: df.groupby('s1').first()
Out[11]:
s2
s1
a 2.0
b NaN
c NaN
In [12]: df.groupby('s1',observed=True).first()
Out[12]:
s2
s1
a 2
In [13]: |
Setup:
Comparing results with
observed=False
andobserved=True
: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)
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: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
The text was updated successfully, but these errors were encountered: