@@ -302,14 +302,16 @@ def groupby(
302
302
and hasattr (table [colname ], "cat" )
303
303
}
304
304
for colname in category_colnames :
305
- table [colname ] = table [colname ].cat .as_ordered ()
305
+ table [colname ].cat .as_ordered (inplace = True )
306
+ # Add dummy "size" to work around
307
+ # https://github.com/pandas-dev/pandas/issues/28641
308
+ agg_sets [colname ].add ("size" )
306
309
307
310
if group_specs :
308
311
# aggs: DataFrame indexed by group
309
312
# out: just the group colnames, no values yet (we'll add them later)
310
- grouped = table .groupby (group_specs )
311
- if agg_sets :
312
- aggs = grouped .agg (agg_sets )
313
+ grouped = table .groupby (group_specs , as_index = True )
314
+ aggs = grouped .agg (agg_sets )
313
315
out = aggs .index .to_frame (index = False )
314
316
# Remove unused categories (because `np.nan` deletes categories)
315
317
for column in out :
@@ -320,8 +322,7 @@ def groupby(
320
322
# aggs: DataFrame with just one row
321
323
# out: one empty row, no columns yet
322
324
grouped = table
323
- if agg_sets :
324
- aggs = table .agg (agg_sets )
325
+ aggs = table .agg (agg_sets )
325
326
out = pd .DataFrame (columns = [], index = [0 ])
326
327
327
328
# Now copy values from `aggs` into `out`. (They have the same index.)
@@ -348,7 +349,7 @@ def groupby(
348
349
349
350
# Remember those category colnames we converted to ordered? Now we need to
350
351
# undo that (and remove newly-unused categories).
351
- for colname in out .columns :
352
+ for colname in list ( out .columns ) :
352
353
column = out [colname ]
353
354
if hasattr (column , "cat" ) and column .cat .ordered :
354
355
column .cat .remove_unused_categories (inplace = True )
0 commit comments