-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle undefined values in arrays #8
base: master
Are you sure you want to change the base?
Conversation
Does this mess up the Array type? I.e. instead of being of type |
Yeah so right now this doesn't work for saving Dicts. import BSON
x = Dict("a" => :a)
BSON.@save "test.bson" x Now quit Julia and re-open it. import BSON
BSON.@load "test.bson" x You get the following error:
|
I think that when we parse a doc, we should skip the nothings. |
I think we should just use BSON's |
Would it also handle
Trying to save it currently crashes Julia. |
@MikeInnes Should we revisit this? It would be really nice to be able to write arrays with undefined values. I agree that we should probably just use the BSON |
@bkamins Saving/loading a Here is the example code for saving: (using the same test case as in your comment) using Pkg
Pkg.add("BSON")
Pkg.add("CategoricalArrays")
Pkg.add("DataFrames")
using BSON, CategoricalArrays, DataFrames
df = DataFrame(x=1:3, b = [:a,missing,:c], c = rand(3), d = categorical(["a","b",missing]))
bson("test.bson", Dict(:df => df)) Now quit and restart Julia, and then run the example code for loading: using BSON, CategoricalArrays, DataFrames
data = BSON.load("test.bson")
data[:df] And the The output of
And the output of
|
Also, I couldn't find an issue tracking this bug specifically (undefined values in arrays), so I opened the following issue: #43 |
In collect_any, Vector{Any}(length(xs)) isn't a valid constructor..., anymore? Im not sure if it used to be or if I am missing something, but I get an error when I run this. I'm not sure there is a way around it. Also what happens if the position of the data matters in an array. It seems like I think all this to say that reworking the splat operator for #undef might be needed and working with the undef sentinel is probably the best way to go. |
There is a small change to how we call the constructor, but apart from that the function collect_any(xs)
ys = Vector{Any}(undef, length(xs))
for i = 1:length(xs)
isassigned(xs, i) && (ys[i] = xs[i])
end
return ys
end And then these examples work exactly as expected: julia> function collect_any(xs)
ys = Vector{Any}(undef, length(xs))
for i = 1:length(xs)
isassigned(xs, i) && (ys[i] = xs[i])
end
return ys
end
collect_any (generic function with 1 method)
julia> a = [1,2,3,4,5]
5-element Array{Int64,1}:
1
2
3
4
5
julia> collect_any(a)
5-element Array{Any,1}:
1
2
3
4
5
julia> b = Array{String}(undef, 5)
5-element Array{String,1}:
#undef
#undef
#undef
#undef
#undef
julia> b[2] = "hello"
"hello"
julia> b[4] = "world"
"world"
julia> b
5-element Array{String,1}:
#undef
"hello"
#undef
"world"
#undef
julia> collect_any(b)
5-element Array{Any,1}:
#undef
"hello"
#undef
"world"
#undef |
I just ran this code on Julia 1.1.1 and it worked as expected. |
K great, that's what I was getting at, the undef part was missing. Thanks |
For now this just writes
undefined
s asnothing
(which works because we don't preserve non-isbits-eltypes anyway). Probably better to use a different sentinel and preserveundefined
-ness through a roundtrip.See #6, cc @DilumAluthge