@@ -375,11 +375,14 @@ def _prepare_threshold_hybrid_frame(
375375 named : dict [str , pd .NamedAgg ] = {}
376376 avg_fields : list [str ] = []
377377 numeric_coerce_fields : list [str ] = []
378+ count_like_fields : list [str ] = []
378379
379380 for vf in value_fields :
380381 agg = aggregation .get (vf , "sum" )
381382 if agg in _NUMERIC_COERCE_AGGS :
382383 numeric_coerce_fields .append (vf )
384+ if agg in ("count" , "count_distinct" ):
385+ count_like_fields .append (vf )
383386 if agg == "avg" :
384387 avg_fields .append (vf )
385388 named [f"{ vf } __sum" ] = pd .NamedAgg (column = vf , aggfunc = "sum" )
@@ -412,6 +415,8 @@ def _prepare_threshold_hybrid_frame(
412415 ser = filtered_df [vf ]
413416 if agg in _NUMERIC_COERCE_AGGS :
414417 ser = _coerce_measure_series (ser , vf , null_handling )
418+ elif agg in ("count" , "count_distinct" ):
419+ ser = _resolve_count_series (ser , vf , null_handling )
415420 if agg == "avg" :
416421 cnt = int (ser .count ())
417422 row [vf ] = float (ser .sum () / cnt ) if cnt else float ("nan" )
@@ -452,6 +457,8 @@ def _prepare_threshold_hybrid_frame(
452457 working [dim ] = _resolve_dim_value_series (working [dim ], col_type , mode , grain )
453458 for vf in numeric_coerce_fields :
454459 working [vf ] = _coerce_measure_series (working [vf ], vf , null_handling )
460+ for vf in count_like_fields :
461+ working [vf ] = _resolve_count_series (working [vf ], vf , null_handling )
455462
456463 out = (
457464 working .groupby (group_fields , dropna = False , observed = True , sort = False )
@@ -663,6 +670,13 @@ def _coerce_measure_series(series: Any, field: str, null_handling: Any) -> Any:
663670 return numeric
664671
665672
673+ def _resolve_count_series (series : Any , field : str , null_handling : Any ) -> Any :
674+ """Preserve values for count-like aggs, zero-filling nulls when requested."""
675+ if _get_null_mode (field , null_handling ) == "zero" :
676+ return series .fillna (0 )
677+ return series
678+
679+
666680def _extract_styler_formats (
667681 styler : Any ,
668682) -> tuple [dict [str , str ], dict [str , str ]]:
0 commit comments