# Fixing GBQ.jl - Dict to DataFrames

**URL:** https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685
**Category:** General Usage
**Tags:** question, package, dataframes
**Created:** [September 23, 2022, 6:12am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685 "2022-09-23T06:12:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)
#### Post date: [September 23, 2022, 6:12am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685/1 "2022-09-23T06:12:31Z")

</div>

Hi All,

I am trying to fix GBQ.jl (as it seems to be the only 100% easy way to access Google Big Query, correct me if I am wrong). When running it with DataFrames 0.27 it works fine, but using the latest version it no longer works (I read somewhere a lot of older libraries stopped working around 1.0?). The error I get is:

```julia
ERROR: MethodError: Cannot `convert` an object of type Dict{Any, Any} to an object of type DataFrame
Closest candidates are:
  convert(::Type{DataFrame}, ::SubDataFrame) at ~/.julia/packages/DataFrames/JZ7x5/src/subdataframe/subdataframe.jl:304
  convert(::Type{T}, ::T) where T at Base.jl:61
  DataFrame(::AbstractDict; copycols) at ~/.julia/packages/DataFrames/JZ7x5/src/dataframe/dataframe.jl:275
  ...
Stacktrace:
 [1] _gbq_parse(response::Vector{Any})
   @ GBQ ~/.julia/packages/GBQ/KCvh5/src/GBQ.jl:60
 [2] gbq_query(query::String; use_legacy_sql::Bool, quiet::Bool, max_rows::Int64)
   @ GBQ ~/.julia/packages/GBQ/KCvh5/src/GBQ.jl:69
 [3] gbq_query(query::String)
   @ GBQ ~/.julia/packages/GBQ/KCvh5/src/GBQ.jl:67
 [4] top-level scope
   @ /home/jupyter/xxxxx/Customer Segmentation.jl:31

```

So from what I can see the data being returned from BQ are dict types and it is trying to convert a Dict to DataFrame - but the conversion fails. When checking the library I assume the error occurs at:

```julia
# internal function to parse json response returned from big query
#
# returns a dataframe
function _gbq_parse(response)
    cols = collect(keys(response[1]))
    values = Dict()
    for key in cols
        values[key] = []
    end
    for dict in response
        for key in cols
            push!(values[key], dict[key])
        end
    end
    return convert(DataFrame, values)
end

# Execute a query
#
# Returns a dataframe
function gbq_query(query; use_legacy_sql=false, quiet=true, max_rows=100000000)
  response = JSON.parse(read(`bq --format=json --quiet="$quiet" query --use_legacy_sql="$use_legacy_sql" --max_rows="$max_rows" "$query"`, String))
  return _gbq_parse(response)
end

```

However, I am not sure what is wrong in the \_gbq\_parse - as the library relies on this extensively…

Is `convert(DataFrame, values)` no longer valid to convert Dict values? I have also tried DataFrames(values) with the same results.

---

<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: [September 23, 2022, 6:50am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685/2 "2022-09-23T06:50:19Z")

</div>

> [@Billpete002](#):
>
> I read somewhere a lot of older libraries stopped working around 1.0?

1.0 was a breaking release of DataFrames.jl.

Here the problem is the incorrect use of `convert` that was in the old DataFrames.jl and was fixed in 1.0 releae.

Now you need to write `DataFrame(values)` instead of `convert(DataFrame, values)`. Assuming that keys of your dict are either strings or `Symbol`s all will work (your dict has key type `Any` so I am not sure if this is the case)

---

<div class="post-metadata">

### Author: ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)
#### Post date: [September 23, 2022, 7:57am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685/3 "2022-09-23T07:57:09Z")

</div>

It still shoots an error when I switch to DataFrame(values) as I briefly mentioned in my first post, this was the first and simplest fix - but when I got the same error I figured I would ask the community:

```julia
ERROR: MethodError: Cannot `convert` an object of type Dict{Any, Any} to an object of type DataFrame
Closest candidates are:
  convert(::Type{DataFrame}, ::SubDataFrame) at ~/.julia/packages/DataFrames/JZ7x5/src/subdataframe/subdataframe.jl:304
  convert(::Type{T}, ::T) where T at Base.jl:61
  DataFrame(::AbstractDict; copycols) at ~/.julia/packages/DataFrames/JZ7x5/src/dataframe/dataframe.jl:275
  ...
Stacktrace:
 [1] _gbq_parse(response::Vector{Any})
   @ GBQ ~/.julia/packages/GBQ/KCvh5/src/GBQ.jl:60
 [2] gbq_query(query::String; use_legacy_sql::Bool, quiet::Bool, max_rows::Int64)
   @ GBQ ~/.julia/packages/GBQ/KCvh5/src/GBQ.jl:69
 [3] gbq_query(query::String)
   @ GBQ ~/.julia/packages/GBQ/KCvh5/src/GBQ.jl:67
 [4] top-level scope
   @ /home/jupyter/xxxx/Customer Segmentation.jl:31

```

---

<div class="post-metadata">

### Author: ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)
#### Post date: [September 26, 2022, 5:27am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685/4 "2022-09-26T05:27:27Z")

</div>

Is there a way to force the JSON response from Big Query to be string or symbol?

---

<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: [September 26, 2022, 6:24am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685/5 "2022-09-26T06:24:16Z")

</div>

But the error has the same reason - somewhere you use `convert(DataFrame, d)` where `d` is your dict, while you should write `DataFrame(d)`. Here is an example:

```julia
julia> d = Dict{Any,Any}("a"=>1:3, "b"=>4:6)
Dict{Any, Any} with 2 entries:
  "b" => 4:6
  "a" => 1:3

julia> DataFrame(d)
3×2 DataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 1 4
   2 │ 2 5
   3 │ 3 6

```

---

<div class="post-metadata">

### Author: ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)
#### Post date: [September 26, 2022, 6:50am UTC](https://discourse.julialang.org/t/fixing-gbq-jl-dict-to-dataframes/87685/6 "2022-09-26T06:50:26Z")

</div>

Closed out of everything and reloaded the changes - apparently saving and reloading the library wasn’t enough.

Thanks! I will update the library back to working order! 🙌 🎉
