JuliaCollections / JuliaCollections/DataStructures.jl

Handle missing better in `DefaultDict`

Open
#530 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
745
Forks
261
PR merge metrics
No merged PRs in 30d

Description

Modified from https://stackoverflow.com/questions/57996377/create-array-of-values-based-on-dictionary-and-array-of-keys:

Say we have a dictionary, and a vector of keys:

X = [2, 1, 1, missing]
d = Dict( 1 => "A", 2 => "B", 3 => "C")

I want to create a new array which contains values instead of keys (according to the dictionary), so the end result would be something like

Y = ["B", "A", "A", "C"]

The basic solution is to modify d and use a comprehension:

d = Dict( 1 => "A", 2 => "B", 3 => "C", missing => missing)
Y = [d[i] for i in X]

I thought I could create a DefaultDict that would handle this slightly nicer, but the end result was less than satisfying:

julia> d = DefaultDict{Union{Int,Missing},Union{String,Missing},Union{String,Missing}}(missing, 1 => "A", 2 => "B", 3 => "C")
ERROR: MethodError: no method matching DefaultDict{Union{Missing, Int64},Union{Missing, String},Union{Missing, String}}(::Missing, ::Pair{Int64,String}, ::Pair{Int64,String}, ::Pair{Int64,String})
Closest candidates are:
  DefaultDict{Union{Missing, Int64},Union{Missing, String},Union{Missing, String}}(::Any, ::Pair{K,V}...; kwargs...) where {K, V, F} at /Users/kevinsquire/.julia/dev/DataStructures/src/default_dict.jl:98
  DefaultDict{Union{Missing, Int64},Union{Missing, String},Union{Missing, String}}(::Any; kwargs...) where {K, V, F} at /Users/kevinsquire/.julia/dev/DataStructures/src/default_dict.jl:105
  DefaultDict{Union{Missing, Int64},Union{Missing, String},Union{Missing, String}}(::Any, ::AbstractArray{Tuple{K,V},N} where N; kwargs...) where {K, V, F} at /Users/kevinsquire/.julia/dev/DataStructures/src/default_dict.jl:100
  ...
Stacktrace:
 [1] top-level scope at REPL[16]:1

julia> d = DefaultDict{Union{Int,Missing},Union{String,Missing},Union{String,Missing}}(missing);

julia> merge!(d, Dict(1 => "A", 2 => "B", 3 => "C"));

julia> Y = [d[i] for i in X]
4-element Array{Union{Missing, String},1}:
 "B"
 "A"
 "A"
 missing

It would be nice to deal with this better, either by supporting missing/Missing in DefaultDict, or in some other way that makes it easy to handle missingness in dictionaries.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the shown constructor error and inspect the DefaultDict constructors in src/default_dict.jl, especially the methods around line 98. Compare the constructor behavior with the merge! and comprehension examples, then determine and document a supported missing/Missing behavior; done means the demonstrated lookup workflow handles missing keys without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.