# Help to check if an element in a Tables.jl is defined

**URL:** <https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110>\
**Category:** General Usage\
**Created:** [August 1, 2020, 10:14pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110 "2020-08-01T22:14:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [August 1, 2020, 10:14pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/1 "2020-08-01T22:14:03Z")

</div>

Hi!

I am trying to print tables that comply with Tables.jl specification. However, this tables can have undefined elements, such as:

```julia
julia> df = DataFrame(B=Int64.(1:9), A = Vector{Any}(undef, 9))

```

I am having a problem to check if an element is undefined using Tables.jl API when I have only row access to the data. Today, I am reading a row table using this code:

```julia
    # Access index.
    i,j = inds[1], inds[2]

    # Get the element.
    it,~ = iterate(table, i)

    it == nothing &&
    error("The row `i` does not exist.")

    element = it[j]

```

It can be tested with DataFrames with this code:

```julia
julia> df = DataFrame(B=Int64.(1:9), A = Vector{Any}(undef, 9))

julia> table = Tables.rows(df)

julia> i,j = 2,2

julia> it,~ = iterate(table, i)

julia> element = it[j]

```

which fails because the element is undefined. My question is: how can I check that this element is undefined to avoid this error? Unfortunately, using `try` and `catch` will have a big performance impact here and I want to avoid it.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 1, 2020, 10:38pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/2 "2020-08-01T22:38:50Z")

</div>

@quinnj - I think for `Tables.row` result we should add a requirement that `isassigned` method should be defined for it.

Also in Tables.jl the `Base.show(io::IO, x::T) where {T <: RorC}` method has `show(io, NamedTuple(x))` line which throws an error on `#undef`.

---

<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 2, 2020, 5:00pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/3 "2020-08-02T17:00:38Z")

</div>

> [@Ronis\_BR](#):
>
> ```julia
> julia> table = Tables.rows(df)
> 
> ```

Hmmmmm…yeah, this is pretty annoying. I think in the data world, we don’t run into this much because these values are usually `missing` instead of `#undef`. I don’t love the idea of requiring `isassigned` because it kind of puts this fault in the entire API; oh, any row value might be `#undef` and I need to check that _everywhere_ to make my code generic? It just feels like we’re turning things into Java where `null` can literally subvert the entire type system and you can’t trust anything.

I’d rather go the other direction and say row values _have_ to be defined; i.e. `#undef` isn’t allowed. Are there cases in the data ecosystem where this actually comes up or is this just for testing? I’d rather go to problematic sources and ensure `#undef` doesn’t come up at all in practice.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 2, 2020, 6:57pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/4 "2020-08-02T18:57:04Z")

</div>

> [@quinnj](#):
>
> Are there cases in the data ecosystem where this actually comes

The only use case I could imagine encountering is when using a table to store simulation data, and the table is created with two columns (simplest case):

- parameters (filled immediately)
- results (set as `#undef` and filled as simulations get run and results from them are collected)

But `#undef` can be easily replaced by `nothing` in this use case.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [August 3, 2020, 12:39pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/5 "2020-08-03T12:39:16Z")

</div>

For my case, I have no preference 🙂 I have to handle `#undef` in arrays anyway, so I still need to check every time if a cell is assigned. I’m good if Tables.jl just prohibit `#undef` entries or if there is an API requirement to define `isassigned`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 3, 2020, 12:56pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/6 "2020-08-03T12:56:15Z")

</div>

My question is if this is even possible without using a sentinel for each field. `isassigned` works when the fields in a struct or the elements of an array point to to a heap-allocated object (because, if I am not wrong, what it does is basically checking if the pointer is null). However, if some of your columns are things like Ints and Floats, and others are heap-allocated objects, then the RowData would need to: either return a partially initialized object (and I remember a recent topic about the problems in trying to do so); or identify columns that may have unassigned elements, create a sentinel for them in the RowData, and specialize the `Base.isassigned` to look for those sentinels instead.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 3, 2020, 2:12pm UTC](https://discourse.julialang.org/t/help-to-check-if-an-element-in-a-tables-jl-is-defined/44110/7 "2020-08-03T14:12:02Z")

</div>

> [@quinnj](#):
>
> I don’t love the idea of requiring `isassigned` because it kind of puts this fault in the entire API; oh, any row value might be `#undef` and I need to check that _everywhere_ to make my code generic? It just feels like we’re turning things into Java where `null` can literally subvert the entire type system and you can’t trust anything.

I agree. I would go with `nothing` or `missing` in practical use, but in case this is not an option, the row iterator could check `isassigned` and return either when needed.
