-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Categorical in GroupBy with aggregations raise error under specific conditions #36698
Comments
It looks like the import pandas as pd
df = pd.DataFrame(
{
"x": pd.Categorical(["a", "b", "c", "a", "b"]),
"y": [1, 1, 1, 2, 2],
"z": [1, 1, 1, 1, 1],
}
)
grouped = df.groupby(["x", "y"], observed=False)
print(grouped.size())
# x y
# a 1 1
# 2 1
# b 1 1
# 2 1
# c 1 1
# 2 0
# dtype: int64
print(grouped.groups.keys()). # ("c", 2) group is missing
# dict_keys([('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1)]) More investigations / PR welcome. |
take |
I ran into the What is the current state of this situation? What is the plan for the future? In pandas 1.3.1 (installed on Debian10.10 via pip) |
When we take the code path for dict-likes within agg, we split up the One possible resolution would be to support |
@rhshadrach Getting the same index error |
As far as I know, no one is currently working on the fix. Further investigations and PRs to fix are welcome.
Use |
Outputs a:
ValueError: Length of values (5) does not match length of index (6)
Full traceback
Problem description
After quite some time trying to narrow down the origin of a
ValueError: Length of values (N) does not match length of index (M)
, it seems to occur, only when these conditions are met:groupby()
done using a categorical variables in theby
listas_index=False
, butas_index=True
is OKobserved=False
, butobserved=True
is OKaggregate()
is performed, while applying directly asum()
on the DataFrameGroupBy is OKsee the different combinations in details below.
Expected Output
Would it be possible to issue an early check and error/exception raise when such conditions are met?
It would definitely help user to understand clearly where the problem comes from and how to correct it.
I am aware that some parts of the issue are being addressed (see PR #35967), but this will not help the user to understand what is actually going on when all conditions are met.
Conditions under which Error is not raised
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 2a7d332
python : 3.8.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : fr_FR.cp1252
pandas : 1.1.2
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: