Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide reproducible Sample Data in Julia

Tags:

julia

Here on stackoverflow.com - when I provide sample data to make a reproducible example, how can I do it the Julian way?

In R for example dput(df) will output a string with which you can create df again. Hence, you just post the result here on stackoverflow and bam! - reproducible example. So, how should one do it in Julia?

like image 829
Georgery Avatar asked May 11 '20 06:05

Georgery


1 Answers

I think the easiest thing to do generally is to simply construct an MWE DataFrame with random numbers etc in your example, so there's no need to read/write out.

In situations where that's inconvenient, you might consider writing out to an IO buffer and taking the string representation of that, which people can then read back in the same way in reverse:

julia> using CSV, DataFrames

julia> df = DataFrame(a = rand(5), b = rand(1:10, 5));

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> string_representation = String(take!(CSV.write(io, df)))
"a,b\n0.5613453808585873,9\n0.3308122459718885,6\n0.631520224612919,9\n0.3533712075535982,3\n0.35289980394398723,9\n"

julia> CSV.read(IOBuffer(string_representation))
5×2 DataFrame
│ Row │ a        │ b     │
│     │ Float64  │ Int64 │
├─────┼──────────┼───────┤
│ 1   │ 0.561345 │ 9     │
│ 2   │ 0.330812 │ 6     │
│ 3   │ 0.63152  │ 9     │
│ 4   │ 0.353371 │ 3     │
│ 5   │ 0.3529   │ 9     │
like image 116
Nils Gudat Avatar answered Oct 23 '22 18:10

Nils Gudat