How to change value of Nullable{WeakRefString{UInt8}} using CSV package?

Hi !

I try to work with the CSV package and I have some problem to change the value of a DataFrame entry because of the Nullable{WeakRefString{UInt8}} type.
I do not know in advance the types of the columns so the default type for String is Nullable{WeakRefString{UInt8}}. I did not find a way to change any value of this type (see example below).

Thanks for your help !

julia> w = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrames.DataFrame
│ Row │ A │ B   │
├─────┼───┼─────┤
│ 1   │ 1 │ "M" │
│ 2   │ 2 │ "F" │
│ 3   │ 3 │ "F" │
│ 4   │ 4 │ "M" │

julia> w[1,2]
"M"

julia> typeof(w[1,2])
String

julia> CSV.write("test/test.tsv", w, delim = sep, header = h)
CSV.Sink(    CSV.Options:
        delim: '	'
        quotechar: '"'
        escapechar: '\\'
        null: ""
        dateformat: dateformat"yyyy-mm-dd", IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1), "test/test.tsv", 8, true, String["A", "B"], false)

julia> w = CSV.read("test/test.tsv"; delim = sep, header = h, null="NA")
4×2 DataFrames.DataFrame
│ Row │ A │ B   │
├─────┼───┼─────┤
│ 1   │ 1 │ "M" │
│ 2   │ 2 │ "F" │
│ 3   │ 3 │ "F" │
│ 4   │ 4 │ "M" │

julia> typeof(w[1,2])
Nullable{WeakRefString{UInt8}}

julia> w[1,2] = "F"
ERROR: type String has no field data
Use `Vector{UInt8}(str)` instead.
Stacktrace:
 [1] setindex!(::NullableArrays.NullableArray{WeakRefString{UInt8},1}, ::String, ::Int64)

You can do CSV.read(file, weakrefstrings=false)

2 Likes

Thank you Quinnj, it solves the problem :wink:

Note that if you don’t have missing data in your files you can also use nullable = false, it will save you some troubles dealing with nullables.

Thank you Jonathan, in my case, I have sometime missing values :slight_smile: