diff --git a/pandas/_libs/hashtable_func_helper.pxi.in b/pandas/_libs/hashtable_func_helper.pxi.in index fdb77a56541de..9744b7f67502f 100644 --- a/pandas/_libs/hashtable_func_helper.pxi.in +++ b/pandas/_libs/hashtable_func_helper.pxi.in @@ -114,7 +114,15 @@ cdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna, const uint8 result_counts[i] = table.vals[k] if na_counter > 0: - assert n > 0 # val will be uninitialized without this + # gcc-11.4 on ubuntu 22.04 cannot see that val won't be unassigned + # when we make it into this branch + assert n > 0 + {{if dtype == 'object'}} + val = values[n - 1] + {{else}} + val = {{to_c_type}}(values[n - 1]) + {{endif}} + result_counts[table.size] = na_counter result_keys.append(val)