Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry with zero values for CategoricalArrays #72

Open
mwsohn opened this issue Jul 20, 2024 · 1 comment
Open

Entry with zero values for CategoricalArrays #72

mwsohn opened this issue Jul 20, 2024 · 1 comment

Comments

@mwsohn
Copy link

mwsohn commented Jul 20, 2024

The following code explains the problem:

using DataFrames, CategoricalArrays, FreqTables
df = DataFrame(id = 1:9, race = repeat(collect(1:3),3))
df.race = categorical(df.race)

freqtable(df,:race)

3-element Named Vector{Int64}
race │
──────┼──
1 │ 3
2 │ 3
3 │ 0

df2 = filter( x -> x.race != 3, df)
freqtable(df2, :race, skipmissing = true)

3-element Named Vector{Int64}
race │
──────┼──
1 │ 3
2 │ 3
3 │ 0

As you can see, df2 does not have any rows with race == 3. But the frequency table reports the entry with zero value. skipmissing option does not affect the output at all.

@bkamins
Copy link
Collaborator

bkamins commented Aug 3, 2024

I think the current behavior is useful, but an option to list only actual levels (not potential levels) would make sense. Maybe droplevels kwarg?

For now you can do:

julia> freqtable(droplevels!(copy(df2.race)))
2-element Named Vector{Int64}
Dim1  │
──────┼──
1     │ 3
2     │ 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants