Open JockLawrie opened 5 years ago
...and the test code
using Utils.jsontables # the code above
using DataFrames
using Dates
using JSON
using TimeZones
d = DataFrame(
a = [1,2,3,missing],
b = ['a', missing, 'c', 'd'],
c = rand(4),
d = Date.(["2018-10-01", "2018-10-02", "2018-10-03", "2010-10-04"])
)
d[:zdt] = [ZonedDateTime(DateTime(x), TimeZone("Australia/Melbourne")) for x in d[:d]]
s = JSON.json(d)
j = JSON.parse(s)
d2 = convert(DataFrame, j)
@test size(d2) == size(d)
@test names(d2) == names(d)
@test eltypes(d2) == eltypes(d)
function cell_values_are_all_equal(d1::DataFrame, d2::DataFrame)
good = true
n = size(d1, 1)
for colname in names(d1)
for i = 1:n
val1 = d1[i, colname]
val2 = d2[i, colname]
ismissing(val1) && ismissing(val2) && continue
bad = (ismissing(val1) && !ismissing(val2)) || (!ismissing(val1) && ismissing(val2)) || (val1 != val2)
if bad == true
good = false
break
end
end
good == false && break
end
good
end
@test cell_values_are_all_equal(d, d2)
Hi there,
I have been using some code for transforming a DataFrame to JSON and back again, with the requirement that the de-JSONified DataFrame is an exact copy of the input DataFrame, eltypes and all. I'd like to make this code public, and see that this package has the same purpose but doesn't preserve types. Can we combine our efforts?
My code is below (...credit where it's due, this was written by Josh Bode).
Cheers, Jock
Parsers for custom types can be added. For example, here's one for
ZonedDateTime
.