# Julia not able to convert Array to a series data for plotting

**URL:** <https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669>\
**Category:** General Usage\
**Tags:** question, array\
**Created:** [March 21, 2021, 8:43pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669 "2021-03-21T20:43:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 8:43pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/1 "2021-03-21T20:43:08Z")

</div>

I am trying to plot a dataframe I created from an existing dataframe. Basically I just took about 5 rows and all N-4 columns and created a new dataframe. I wanted to plot this but Julia (on Pluto.jl) is throwing this error:

```julia
Cannot convert Array{Any,2} to series data for plotting

error(::String)@error.jl:33
_prepare_series_data(::Array{Any,2})@series.jl:8
_series_data_vector(::Array{Any,2}, ::Dict{Symbol,Any})@series.jl:27
macro expansion@series.jl:144[inlined]
apply_recipe(::AbstractDict{Symbol,Any}, ::Type{RecipesPipeline.SliceIt}, ::Any, ::Any, ::Any)@RecipesBase.jl:282
_process_userrecipes!(::Any, ::Any, ::Any)@user_recipe.jl:36
recipe_pipeline!(::Any, ::Any, ::Any)@RecipesPipeline.jl:70
_plot!(::Plots.Plot, ::Any, ::Any)@plot.jl:172
#plot#129@plot.jl:58[inlined]
#add_label#17(::Base.Iterators.Pairs{Symbol,Any,NTuple{13,Symbol},NamedTuple{(:label, :xlabel, :ylabel, :xticks, :xrotation, :marker, :line, :legend, :grid, :framestyle, :legendfontsize, :tickfontsize, :formatter),Tuple{Array{String,2},String,String,Array{Dates.Date,1},Int64,Tuple{Symbol,Int64},Tuple{Symbol,String},Symbol,Bool,Symbol,Int64,Int64,Symbol}}}, ::typeof(StatsPlots.add_label), ::Array{Any,1}, ::Function, ::Array{Dates.Date,1}, ::Vararg{Any,N} where N)@df.jl:155
(::Main.workspace140.var"#1#3")(::DataFrames.DataFrame)@range.jl:0
top-level scope@Local: 17

```

This is the cell that is throwing this error:

```julia
begin
    
    countries = ["Italy", "Germany", "India", "United Kingdom"];
    y = DataFrame() # empty dataframe

    for country in countries    
        data_dfr = get_country(df,country); # returns a dataframe row 
        data_dfr = DataFrame(data_dfr); # convert dataframe row back to a dataframe
        df_rows, df_cols = size(data_dfr);
        data_dfl = stack(data_dfr, 5:df_cols); # convert dataframe into long format
        y[!,Symbol("$country")] = data_dfl[!,:value]
    end

    rows,cols = size(y)
    
    gr(size=(900,600))
    @df y plot(x_axis, cols(1:cols), 
        label = reshape(names(y),(1,length(names(y)))),
        xlabel = "Time",
        ylabel = "Total number of reported cases",
        xticks = x_axis[1:7:end],
        xrotation = 45,
        marker = (:diamond,4),
        line = (:line, "gray"),
        legend = :topleft,
        grid = false,
        framestyle = :semi,
        legendfontsize = 9,
        tickfontsize = 9,
        formatter = :plain
        )
    
    y.One_million = Array{Union{Missing,Float64},1}(missing,size(y,1));
    y.One_million .= 10^6.0;
    
    display(@df y plot!(x_axis, y[!,cols+1],
           linestyle = :dot,
           linewidth = 5,
           color = :red,
           label = names(y)[cols+1]))
    
    y = select!(y, Not([:One_million])); 
end

```

Here are the functions and variables used in the above code:

```julia
begin
    dates = names(df)[begin:end-4]
    date_format = Dates.DateFormat("m/d/y")
    x_axis = parse.(Date, dates, date_format) .+ Year(2000)
end

```

```julia
function get_country(dataframe, country::String)
    df_country = dataframe[ismissing.(dataframe[!, Symbol("Province/State")]), :]
    indx = findfirst(df_country[!, Symbol("Country/Region")] .== country)
    return df_country[indx, :]
end

```

```julia
begin
	dataset = download("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv","covid_19_global_data.csv")
	
	df = CSV.read("covid_19_global_data.csv", DataFrame)
	write_parquet("covid_19_global_data.parquet", df)
	df = DataFrame(read_parquet("covid_19_global_data.parquet"))
	Arrow.write("covid_19_global_data.arrow", df)
	df = DataFrame(Arrow.Table("covid_19_global_data.arrow"))
		
end

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 21, 2021, 9:01pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/2 "2021-03-21T21:01:16Z")

</div>

I couldn’t run your code because df is not defined. Could you try and define the rest of the variables?

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 9:03pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/3 "2021-03-21T21:03:09Z")

</div>

Done. Check the edits.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 21, 2021, 9:36pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/4 "2021-03-21T21:36:43Z")

</div>

Hmmm. I tried for 20 minutes to replicate your problem but I couldn’t. Part of it is because you use many packages that are not in your snippet. This made me have to investigate what packages I needed to install to be able to run every line. After discovering that I had to install and use Arrow.jl, CSV.jl, and Parquet.jl, I stopped looking because there are further packages I need to install to get to the point of your error. I advice to take a look at [PSA: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) for advice on how to paste a minimal working example, it would be easier to reproduce your error and help.  
Ideally we should be able to copy and paste your snippet and it should throw the error.

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 10:03pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/5 "2021-03-21T22:03:51Z")

</div>

Well, did you get a graph output? What did you end up getting?

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 11:13pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/7 "2021-03-21T23:13:15Z")

</div>

I do not want the last 4 columns. Your code seems to avoid the 1st 4 columns.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 21, 2021, 11:25pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/8 "2021-03-21T23:25:23Z")

</div>

The last 4 rows seem to have non-numerical data. Could you double-check?

```julia
julia> y
424×4 DataFrame
 Row │ Italy Germany India United Kingdom 
     │ Any Any Any Any
─────┼────────────────────────────────────────────
   1 │ 2319036 1993892 10512093 3211576        
   2 │ 2336279 2015235 10527683 3260258        
   3 │ 2352423 2023828 10542841 3316019
   4 │ 2368733 2038645 10557985 3357361
   5 │ 2381277 2050129 10571773 3395959
   6 │ 2390102 2059382 10581823 3433494
  ⋮ │ ⋮ ⋮ ⋮ ⋮
 420 │ 281583 256433 4465863 355219
 421 │ Italy Germany India United Kingdom
 422 │ 41.8719 51.1657 20.5937 55.3781
 423 │ 12.5674 10.4515 78.9629 -3.436
 424 │ missing missing missing missing   

```

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 11:30pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/9 "2021-03-21T23:30:43Z")

</div>

Yep. That’s why I don’t want them. This df was created from an arrow file, at the end of the day. It reversed the ordering of the columns from when it was a CSV.

How can I use, from begin:end, only the numerical data?

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [March 21, 2021, 11:37pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/10 "2021-03-21T23:37:49Z")

</div>

For the dataframe posted above:

```julia
plot(filter(x -> isa(x, Number), y.Italy))

```

would plot the `Italy` column.

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 11:39pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/11 "2021-03-21T23:39:19Z")

</div>

What if I want to plot all of them?

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [March 21, 2021, 11:43pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/12 "2021-03-21T23:43:53Z")

</div>

One way would be to iterate over all the columns:

```julia
plot()
for colname in names(y)
    plot!(filter(x -> isa(x, Number), y[!, Symbol(colname)]), label = colname, show = true)
end

```

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 21, 2021, 11:44pm UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/13 "2021-03-21T23:44:42Z")

</div>

Sure but my current code is good. How can I improve my existing code?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 22, 2021, 12:21am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/14 "2021-03-22T00:21:32Z")

</div>

Check if this helps:

```julia
i = 1; n = rows
while i <= n
    if prod(isa.(collect((y)[i,:]),Number))==0
       delete!(y,i)
       x_axis = x_axis[1:end .!= i]
       n -= 1
    end
    i += 1
end

```

_PS: the plot does not look very nice though:_

 ![Figure1_covid19](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5fd805919246f967abab0f67a24b98681a46b61.png)

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 22, 2021, 12:24am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/15 "2021-03-22T00:24:54Z")

</div>

But where in the code does this go to? Or rather, what do I replace with this?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 22, 2021, 12:26am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/16 "2021-03-22T00:26:39Z")

</div>

You should place it between these two lines:

```julia
rows, cols = size(y)
....
gr(size = (900, 600))

```

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 22, 2021, 12:27am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/17 "2021-03-22T00:27:36Z")

</div>

What about my existing plot code that starts here:

```julia
@df y plot(x_axis, cols(1:cols), 

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 22, 2021, 12:33am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/18 "2021-03-22T00:33:00Z")

</div>

If you had tried the code you would have gotten for the second plot:

 ![Figure2_covid19](https://global.discourse-cdn.com/julialang/original/3X/e/9/e9def03dae006b4ad65936d8831bdd0629ef6ddc.png)

_PS: most of us do not use discourse as a chat tool. There are dedicated tools for that purpose._

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 22, 2021, 12:35am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/19 "2021-03-22T00:35:45Z")

</div>

```julia
UndefVarError: i not defined

```

Which is weird because you’re clearly defining i

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 22, 2021, 1:15am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/20 "2021-03-22T01:15:32Z")

</div>

Sounds like problem of local vs global scope. May need to declare as `global` in the `while` loop.

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 22, 2021, 1:23am UTC](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669/21 "2021-03-22T01:23:08Z")

</div>

I did this:

```julia
	global i = 1 
	
	n = rows
	while i <= n
    	if prod(isa.(collect((y)[i,:]),Number))==0
       		delete!(y,i)
       		x_axis = x_axis[1:end .!= i]
       		n -= 1
    	end
    	i += 1
	end

```

Same error. When I did `while global i <= n`, it threw a syntax error.

[Next page](https://discourse.julialang.org/t/julia-not-able-to-convert-array-to-a-series-data-for-plotting/57669.md?page=2)
