(Beginner) How to access value in a DataValue

At DataValues.jl, I am informed that " One can access or unpack the value within a DataValue either via the get(x) function, or use the x[] syntax." I do not know “the x[] syntax” - when I tried just writing (e.g.) i.key[], I got a nasty error.

Going to Query.jl,which claims at its bottom “The package is completely documented.”, I looked for DataValue - there is some text about missing values in Getting started, but nothing whatsoever about some [] syntax. Should it be there, or where else would I look?

Last, it might be that I have another sort of (understanding or code) problem. What I do, is:

    fromdata = DataFrame(@from i in ... begin ... @select { i.key, i.xmin,  ... } end)
    key2x = Dict(collect(@from i in fromdata begin @select i.key => i.xmin end))

and then later, in a PyPlot diagram,

    for (k, x) in pairs(key2x)
        annotate("   $k", (key2x[k], minimum(fromdata.min)), size="xx-small", rotation="vertical")

I expected the $k to be the key value - however, I get the key and then appended “:DataValue{String}()”. I assumed that k is thus not a “naked string”, but a string boxed in a DataValue. However, when I replace $k with $(get(k)) (according to the doc of DataValue), I get an error

MethodError: no method matching get(::String)

So k is a String - but why, then, does my annotation in the diagram have this type appendix??

Thanks for any clarification!

Can you clarify what your fromdata structure is? A DataFrame? Dict? Something else?

It’s a DataFrame - I added a respective snippet in my original question.

… problem solved - it was a few miles upstream. There, I created the key like that

   key = ... * string(i.something) * ...

i.something is a DataValue; and string() produces that “value:type”, which is :DataValue{String}() for a missing string. What I actually want(ed), is

   key = ... * (isna(i.something) ? "" : get(i.something)) * ...

And then all is fine.
So my long question was quite unnecessary and somewhat embarassing - well, “(Beginner)” ;-). Maybe it should be deleted. Thanks anyway for asking back!

A shorter version is get(i.something, ""). That will return a String no matter what: if i.something has a value it will be that String, if it doesn’t have a value it will return "".

Thank you - I guessed something like that, but I looked DataValues…examples, and there I saw only the isna() version (at 03-combined).

Yeah, I really should write more docs over at DataValues.jl… I had originally thought it would be more of a temporary package, but not it very much seems it will stay around for much longer, so probably worth documenting it properly.

1 Like