A categorical column with 20 categories is grouped, but only 3 categories occur in the data. Why does observed=True shrink the result?
Correct answer
D. Because the default expands every category combination, producing empty groups too
14 views
D. Because the default expands every category combination, producing empty groups too
14 views
Categorical groupby keeps the full category set by default, so unobserved categories appear as empty groups, and with two categorical keys the result becomes their cartesian product. Passing observed=True restricts the result to combinations actually present, saving memory and time. pandas 3.0 changes the default to True, so code relying on the old behaviour may need updating.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is D? Ask here and get an explanation.