Handle common_norm for density stat in Hist#3911
Open
Mr-Neutr0n wants to merge 1 commit intomwaskom:masterfrom
Open
Handle common_norm for density stat in Hist#3911Mr-Neutr0n wants to merge 1 commit intomwaskom:masterfrom
Mr-Neutr0n wants to merge 1 commit intomwaskom:masterfrom
Conversation
Previously, Hist(stat='density') passed density=True directly to np.histogram, which normalized each group independently before common_norm could take effect. This meant common_norm=True was silently ignored for density normalization. Move density normalization into _normalize() so it respects the same common_norm grouping logic used by other stats like percent and proportion. Closes mwaskom#3633
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
so.Hist(stat='density', common_norm=True)was silently ignoringcommon_norm— each group's density was normalized independently instead of across all groups.The root cause:
_eval()passeddensity=Truetonp.histogram, which baked in per-group normalization before_normalize()(and itscommon_normgrouping logic) ever ran. For every other stat (percent,proportion,frequency), normalization happens inside_normalize()wherecommon_normis properly respected — butdensitywas the exception.Changes
_eval(): Always passdensity=Falsetonp.histogramso raw counts are preserved for all stats._normalize(): Add density normalization (count / (total_count * bin_width)) alongside the existing percent/proportion/frequency cases. This waycommon_normcontrols the scope oftotal_countfor density just like it does for the other stats.Test plan
test_common_norm_density_default— verifies total area sums to 1 across all groups whencommon_norm=Truetest_common_norm_density_false— verifies each group individually sums to 1 whencommon_norm=Falsetest_common_norm_density_subset— verifies normalization within subgroupstest_counting.pypass (29 existing + 3 new)Closes #3633