# (Beginner) How to access value in a DataValue

**URL:** <https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944>\
**Category:** Data\
**Created:** [August 24, 2019, 6:12pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944 "2019-08-24T18:12:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 24, 2019, 6:12pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/1 "2019-08-24T18:12:32Z")

</div>

At [DataValues.jl](https://github.com/queryverse/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](https://github.com/queryverse/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](http://www.queryverse.org/Query.jl/stable/gettingstarted/), 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:

```julia
    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,

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

```

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!  
H.M.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [August 24, 2019, 7:02pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/2 "2019-08-24T19:02:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 24, 2019, 7:05pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/3 "2019-08-24T19:05:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 24, 2019, 7:21pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/4 "2019-08-24T19:21:06Z")

</div>

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

```julia
   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

```julia
   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!

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [August 24, 2019, 7:40pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/5 "2019-08-24T19:40:32Z")

</div>

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 `""`.

---

<div class="post-metadata">

**Author:** ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)\
**Post date:** [August 24, 2019, 7:44pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/6 "2019-08-24T19:44:03Z")

</div>

Thank you - I guessed something like that, but I looked [DataValues…examples](https://github.com/queryverse/DataValues.jl/tree/master/examples), and there I saw only the isna() version (at 03-combined).

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [August 24, 2019, 7:47pm UTC](https://discourse.julialang.org/t/beginner-how-to-access-value-in-a-datavalue/27944/7 "2019-08-24T19:47:31Z")

</div>

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.
